const optionsAgency = [
  {label: '代配', value: '0'},
  {label: '代煎', value: '1'},
  {label: '自煎', value: '2'},
];
const optionsDelivery = [
  {label: '配送到本院（站）', value: '1'},
  {label: '快递配送', value: '0'},
];

export const analysisAgency = (data) => {
  const defaultAgency = (data.defaultAgency || '').toString().trim();
  const displayAgency = (data.displayAgency || '').toString().trim();
  // 保留原有逻辑
  if (/^\d+/.test(displayAgency)) return {
    // 默认代煎 0是 1否
    defaultAgency: defaultAgency === '0' ? '1' : '2',
    // 显示代煎 0显示 1隐藏
    displayAgency: displayAgency === '1' ? [] : optionsAgency.filter(option => option.value !== '2').map(option => option.value),
    optionsAgency: [...optionsAgency],
  };
  // 存在新值 (label:value,label:value)
  if (displayAgency) {
    const options = [...optionsAgency];
    const values = [];
    for (const string of displayAgency.split(',').filter(Boolean)) {
      const [label, value] = string.split(':');
      const option = options.find((option) => option.value === value);
      if (label) {
        if (option) option.label = label;
        else options.push({ label, value });
      }
      values.push(value);
    }
    return {
      defaultAgency: defaultAgency,
      displayAgency: values,
      optionsAgency: options,
    };
  }
  // 默认配置
  return {
    defaultAgency: defaultAgency,
    displayAgency: [],
    optionsAgency,
  };
};

export const analysisDelivery = (data) => {
  const defaultDelivery = (data.defaultDelivery || '').toString().trim();
  const displayDelivery = (data.displayDelivery || '').toString().trim();
  // 保留原有逻辑
  if (/^\d+/.test(displayDelivery)) return {
    // 默认配送  0是 1否
    defaultDelivery: defaultDelivery,
    // 显示配送 0显示 1隐藏
    displayDelivery: displayDelivery === '1' ? [] : optionsDelivery.map(option => option.value),
    optionsDelivery: optionsDelivery,
  };
  // 存在新值 (label:value,label:value)
  if (displayDelivery) {
    const options = [...optionsDelivery];
    const values = [];
    for (const string of displayDelivery.split(',').filter(Boolean)) {
      const [label, value] = string.split(':');
      const option = options.find((option) => option.value === value);
      if (label) {
        if (option) option.label = label;
        else options.push({ label, value });
      }
      values.push(value);
    }
    return {
      defaultDelivery: defaultDelivery,
      displayDelivery: values,
      optionsDelivery: options,
    };
  }
  // 默认配置
  return {
    defaultDelivery: defaultDelivery,
    displayDelivery: [],
    optionsDelivery,
  };
}