export const defaultCellProperties = {
  text: {
    leftParentCellType: 'none',
    leftParentCellCustomRowName: '',
    leftParentCellCustomColName: '',
    topParentCellType: 'none',
    topParentCellCustomRowName: '',
    topParentCellCustomColName: '',
  },
  parameter: {
    field: '',
    leftParentCellType: 'none',
    leftParentCellCustomRowName: '',
    leftParentCellCustomColName: '',
    topParentCellType: 'none',
    topParentCellCustomRowName: '',
    topParentCellCustomColName: '',
  },
  dataSource: {
    field: '',
    polymerizationType: '1',
    summaryType: 'sum',
    groupType: 'default',
    fillDirection: 'portrait',
    leftParentCellType: 'default',
    leftParentCellCustomRowName: '',
    leftParentCellCustomColName: '',
    topParentCellType: 'default',
    topParentCellCustomRowName: '',
    topParentCellCustomColName: '',
    fillEmptyRows: false,
    fillEmptyNum: 1,
    displayType: 'default',
    qrCodeOption: {
      color: {
        dark: '#000000',
        light: '#f4f5f6',
      },
      errorCorrectionLevel: 'L',
    },
    jsbarcodeOption: {
      format: 'code128',
      displayValue: false,
      lineColor: '#000000',
      background: '#f4f5f6',
      width: 4,
      margin: 15,
      font: 'Arial',
      fontSize: 18,
      textAlign: 'center',
      textPosition: 'bottom',
    },
  },
  qrCode: {
    field: '二维码',
    qrCodeOption: {
      type: 'static',
      color: {
        dark: '#000000',
        light: '#f4f5f6',
      },
      errorCorrectionLevel: 'M',
    },
  },
  jsbarcode: {
    field: '10241024',
    jsbarcodeOption: {
      type: 'static', // 这里预留动态
      format: 'code128', // 设置条形码类型为 EAN13
      displayValue: false, // 显示数字
      lineColor: '#000000', // 设置线条为蓝色
      background: '#f4f5f6', // 设置背景色为浅灰色
      width: 4, // 设置条形码宽度
      margin: 15, // 设置条形码四周边距
      font: 'Arial', // 设置字体
      fontSize: 18, // 设置字体大小
      textAlign: 'center', // 设置文本居中
      textPosition: 'bottom', // 设置文本在条形码底部
    },
  },
};

// 拼接url
export function getFloatImageUrl(data, url) {
  for (const key in data) {
    const type = data[key].source;
    const src = data[key].option.src;
    if (src && type == 1) data[key].option.src = url + src;
  }
  return data;
}

// 拼接快照url
export function getFloatUrl(list, reportApiUrl, floatImages = {}) {
  const item: any = {};
  if (!list.length) return;

  for (const [_index, element] of list.entries()) {
    if (element.name === 'SHEET_DRAWING_PLUGIN') {
      element.data = element.data ? JSON.parse(element.data) : {};
      for (const key in element.data) {
        for (const imageKey in element.data[key].data) {
          const componentKey = element.data[key].data[imageKey].componentKey;
          if (componentKey == 'JnpfUniverFloatEchart') continue;

          const source = floatImages[imageKey]?.option?.src;

          if (source) {
            element.data[key].data[imageKey].source = source;
            element.data[key].data[imageKey].imageSourceType = 'BASE64';
          }
        }
      }
      element.data = JSON.stringify(element.data);
    }
  }
  item.list = list;
  item.floatImages = floatImages;
  return item;
}

// 截取url前缀
export function interceptUrl(data, reportApiUrl) {
  for (const key in data) {
    const type = data[key].source;
    let src = data[key].option.src;
    if (src && type == 1) {
      src = src.split(reportApiUrl)[1];
      data[key].option.src = src;
    }
  }
  return data;
}

/**
 * 根据索引值获取字母字符串。
 * @param index {number} - 输入的索引值（如 0, 1, 26, 27 等）。
 * @returns {string} - 对应的字母字符串（如 "A", "B", "AA", "AB" 等）。
 */
export const getAlphabetFromIndexRule = (index: number): string => {
  const base = 26; // 字母表的长度
  const charCodeA = 'A'.codePointAt(0);

  let result = '';
  index += 1; // 转为从 1 开始的规则

  while (index > 0) {
    const remainder = (index - 1) % base;
    result = String.fromCodePoint((charCodeA || 0) + remainder) + result;
    index = Math.floor((index - 1) / base);
  }

  return result;
};
