import type {
  SpreadsheetColumnConfig,
  SpreadsheetHeaderConfig,
  SpreadsheetTextAlign,
} from '#/components/canvas-spreadsheet';

export const WAREHOUSE_SPREADSHEET_TABLE_NAME = '中药仓库-总仓-价格';

export function warehouseSpreadsheetSheetKey(productType: number) {
  return `type_${productType}`;
}

export interface SpreadsheetConfigJson {
  headers: SpreadsheetHeaderConfig[];
  columns: SpreadsheetColumnConfig[];
  defaultColWidth?: number;
  defaultTextAlign?: SpreadsheetTextAlign;
  enableFilter?: boolean;
  enableHistory?: boolean;
  rowHeight?: number | 'auto';
}

export function getWarehouseSpreadsheetDefaults(
  productType: number,
): SpreadsheetConfigJson {
  return {
    defaultColWidth: 120,
    defaultTextAlign: 'left',
    enableFilter: true,
    enableHistory: true,
    rowHeight: 'auto',
    headers: [
      {
        title: '药品名称',
        col: 0,
        fieldId: 'drug_name',
        filterable: true,
        fixed: true,
        headerColor: '#f0f5ff',
      },
      { title: '拼音', col: 1, fieldId: 'pinyin_simple', filterable: true },
      { title: '货号', col: 2, fieldId: 'drug_number', filterable: true },
      {
        title: '供货价',
        col: 3,
        fieldId: 'market_price',
        filterable: true,
        headerColor: '#fff7e6',
      },
      { title: '建议售价', col: 4, fieldId: 'price', filterable: true },
      {
        title: '价差',
        col: 5,
        fieldId: 'price_diff',
        filterable: false,
        headerColor: '#f6ffed',
      },
    ],
    columns: [
      {
        fieldId: 'drug_name',
        fieldName: '药品名称',
        cellType: 'readonly',
        col: 0,
        editable: false,
      },
      {
        fieldId: 'pinyin_simple',
        fieldName: '拼音',
        cellType: 'readonly',
        col: 1,
        editable: false,
      },
      {
        fieldId: 'drug_number',
        fieldName: '货号',
        cellType: 'readonly',
        col: 2,
        editable: false,
      },
      {
        fieldId: 'market_price',
        fieldName: '供货价',
        cellType: 'number',
        col: 3,
        editable: true,
      },
      {
        fieldId: 'price',
        fieldName: '建议售价',
        cellType: 'number',
        col: 4,
        editable: true,
      },
      {
        fieldId: 'price_diff',
        fieldName: '价差',
        cellType: 'formula',
        col: 5,
        editable: false,
        formula: '=(E1-D1)',
      },
    ],
  };
}

export function mergeSpreadsheetConfig(
  remote: Partial<SpreadsheetConfigJson> | null | undefined,
  defaults: SpreadsheetConfigJson,
): SpreadsheetConfigJson {
  if (!remote) return { ...defaults };
  return {
    defaultColWidth: remote.defaultColWidth ?? defaults.defaultColWidth,
    defaultTextAlign: remote.defaultTextAlign ?? defaults.defaultTextAlign,
    enableFilter: remote.enableFilter ?? defaults.enableFilter,
    enableHistory: remote.enableHistory ?? defaults.enableHistory,
    rowHeight: remote.rowHeight ?? defaults.rowHeight,
    headers:
      Array.isArray(remote.headers) && remote.headers.length > 0
        ? remote.headers
        : defaults.headers,
    columns:
      Array.isArray(remote.columns) && remote.columns.length > 0
        ? remote.columns
        : defaults.columns,
  };
}
