import type { ElementGroup } from './types';
import { buildOperandSet } from './parser';

/** 模板选项 — 预设表达式 */
export const DEFAULT_TEMPLATE_OPTIONS: { label: string; value: string }[] = [
  { label: '压差预警1', value: '20<=sourcePressureChange && sourcePressureChange<=40' },
  { label: '压差预警2', value: '40<sourcePressureChange && sourcePressureChange<=60' },
  { label: '压差预警3', value: '60<sourcePressureChange && sourcePressureChange<=80' },
  { label: '压差预警4', value: '80<sourcePressureChange' },
  { label: '火灾预警1', value: '((coRzl<=20||coZf<=0.5*coAvg)&&coVal<=10)||o2Val<=5&&temperatureDifference<2&&co2Val<0.5' },
  {
    label: '火灾预警2',
    value:
      '((coRzl>20&&coRzl<=50)||(0.5*coAvg<coZf&&coZf<=coAvg)||(coVal>10&&coVal<=50)||(temperatureDifference>=2&&temperatureDifference<=4))&&co2Val<0.5',
  },
  {
    label: '火灾预警3',
    value:
      '((coRzl>50&&coRzl<=100)||(coAvg<coZf&&coZf<=2*coAvg)||(coVal>=50&&coVal<=100)||(temperatureDifference>4&&temperatureDifference<=10))&&co2Val<0.5',
  },
  { label: '火灾预警4', value: 'coRzl>100||2*coAvg<coZf||coVal>100||temperatureDifference>10||(co2Val>=0.5&&co2Trend)' },
  { label: '密闭墙外火灾预警1', value: 'temperatureDifference<2||ch4Val<0.5||coVal<5||o2Val<20' },
  { label: '密闭墙外火灾预警2', value: 'temperatureDifference>=2&&temperatureDifference<=4||ch4Val>=0.5&&ch4Val<=1||coVal>=5&&coVal<10' },
  {
    label: '密闭墙外火灾预警3',
    value: 'temperatureDifference>4&&temperatureDifference<=10&&temperature>=26||ch4Val>=1&&ch4Val<1.5||coVal>=10&&coVal<24||c2h4Val>0&&c2h2Val==0',
  },
  { label: '密闭墙外火灾预警4', value: 'temperatureDifference>10&&temperature>=30||ch4Val>=1.5&&ch4Val<2||coVal>=24||c2h2Val>0' },
  {
    label: 'OPEN',
    value:
      '(temperature<30||-3<temperature-fireAirTemperature&&temperature-fireAirTemperature<3)&&o2Val<5&&c2h4Val<0.001&&c2h2Val<0.001&&coVal<0.001&&(temperature<25||-3<temperature-fireWaterTemperature&&temperature-fireWaterTemperature<3)&&stableDays>30',
  },
];

/** 元素库分组 — 定义表达式中可用的变量、运算符、逻辑符、括号和数值 */
export const DEFAULT_ELEMENT_GROUPS: ElementGroup[] = [
  {
    key: 'gas',
    header: '监测变量',
    type: 'operand',
    items: [
      { type: 'operand', value: 'sourcePressureChange', label: '压差变化' },
      { type: 'operand', value: 'coRzl', label: 'CO日增率' },
      { type: 'operand', value: 'coZf', label: 'CO增幅' },
      { type: 'operand', value: 'coAvg', label: 'CO平均值' },
      { type: 'operand', value: 'coVal', label: 'CO值' },
      { type: 'operand', value: 'co2Val', label: 'CO2值' },
      { type: 'operand', value: 'co2Trend', label: 'CO2存在上升趋势' },
      { type: 'operand', value: 'o2Val', label: 'O2值' },
      { type: 'operand', value: 'ch4Val', label: 'CH4值' },
      { type: 'operand', value: 'c2h4Val', label: 'C2H4值' },
      { type: 'operand', value: 'c2h2Val', label: 'C2H2值' },
      { type: 'operand', value: 'temperature', label: '温度' },
      { type: 'operand', value: 'fireAirTemperature', label: '气温' },
      { type: 'operand', value: 'fireWaterTemperature', label: '水温' },
      { type: 'operand', value: 'temperatureDifference', label: '温差' },
      { type: 'operand', value: 'stableDays', label: '稳定天数' },
      { type: 'operand', value: 'sourcePressureChangeOut', label: '密闭墙外压差变化' },
      { type: 'operand', value: 'coRzlOut', label: '密闭墙外CO日增率' },
      { type: 'operand', value: 'coZfOut', label: '密闭墙外CO增幅' },
      { type: 'operand', value: 'coAvgOut', label: '密闭墙外CO平均值' },
      { type: 'operand', value: 'coValOut', label: '密闭墙外CO值' },
      { type: 'operand', value: 'co2ValOut', label: '密闭墙外CO2值' },
      { type: 'operand', value: 'co2TrendOut', label: '密闭墙外CO2存在上升趋势' },
      { type: 'operand', value: 'o2ValOut', label: '密闭墙外O2值' },
      { type: 'operand', value: 'ch4ValOut', label: '密闭墙外CH4值' },
      { type: 'operand', value: 'c2h4ValOut', label: '密闭墙外C2H4值' },
      { type: 'operand', value: 'c2h2ValOut', label: '密闭墙外C2H2值' },
      { type: 'operand', value: 'temperatureOut', label: '密闭墙外温度' },
      { type: 'operand', value: 'fireAirTemperatureOut', label: '密闭墙外气温' },
      { type: 'operand', value: 'fireWaterTemperatureOut', label: '密闭墙外水温' },
      { type: 'operand', value: 'temperatureDifferenceOut', label: '密闭墙外温差' },
      { type: 'operand', value: 'stableDaysOut', label: '密闭墙外稳定天数' },
    ],
    extra: [],
  },
  {
    key: 'operator',
    header: '计算符',
    type: 'operator',
    items: [
      { type: 'operator', value: '>', label: '>' },
      { type: 'operator', value: '>=', label: '≥' },
      { type: 'operator', value: '<', label: '<' },
      { type: 'operator', value: '<=', label: '≤' },
      { type: 'operator', value: '==', label: '=' },
      { type: 'operator', value: '+', label: '+' },
      { type: 'operator', value: '-', label: '-' },
      { type: 'operator', value: '*', label: '*' },
      { type: 'operator', value: '/', label: '/' },
    ],
    extra: [],
  },
  {
    key: 'logic',
    header: '逻辑符',
    type: 'logic',
    items: [
      { type: 'logic', value: '&&', label: '且' },
      { type: 'logic', value: '||', label: '或' },
    ],
    extra: [],
  },
  {
    key: 'paren',
    header: '括号',
    type: 'paren',
    items: [
      { type: 'leftParen', value: '(', label: '(' },
      { type: 'rightParen', value: ')', label: ')' },
    ],
    extra: [],
  },
  {
    key: 'number',
    header: '数值',
    type: 'number',
    items: [
      { type: 'number', value: '0', label: '0' },
      { type: 'number', value: '10', label: '10' },
      { type: 'number', value: '20', label: '20' },
      { type: 'number', value: '30', label: '30' },
      { type: 'number', value: '40', label: '40' },
      { type: 'number', value: '50', label: '50' },
      { type: 'number', value: '60', label: '60' },
      { type: 'number', value: '70', label: '70' },
      { type: 'number', value: '80', label: '80' },
      { type: 'number', value: '90', label: '90' },
      { type: 'number', value: '100', label: '100' },
    ],
    extra: [{ type: 'number', value: '0', label: '追加自定义值' }],
  },
];

/** 显示名称映射 — 变量名/运算符 → 中文标签 */
export const DEFAULT_DISPLAY_MAP: Record<string, string> = {
  '&&': '且',
  '||': '或',
  '>=': '≥',
  '<=': '≤',
  '==': '=',
  sourcePressureChange: '压差变化',
  coRzl: 'CO日增率',
  coZf: 'CO增幅',
  coAvg: 'CO平均值',
  coVal: 'CO值',
  co2Val: 'CO2值',
  co2Trend: 'CO2存在上升趋势',
  o2Val: 'O2值',
  ch4Val: 'CH4值',
  c2h4Val: 'C2H4值',
  c2h2Val: 'C2H2值',
  temperature: '温度',
  fireAirTemperature: '气温',
  fireWaterTemperature: '水温',
  temperatureDifference: '温差',
  stableDays: '稳定天数',
  sourcePressureChangeOut: '密闭墙外压差变化',
  coRzlOut: '密闭墙外CO日增率',
  coZfOut: '密闭墙外CO增幅',
  coAvgOut: '密闭墙外CO平均值',
  coValOut: '密闭墙外CO值',
  co2ValOut: '密闭墙外CO2值',
  co2TrendOut: '密闭墙外CO2存在上升趋势',
  o2ValOut: '密闭墙外O2值',
  ch4ValOut: '密闭墙外CH4值',
  c2h4ValOut: '密闭墙外C2H4值',
  c2h2ValOut: '密闭墙外C2H2值',
  temperatureOut: '密闭墙外温度',
  fireAirTemperatureOut: '密闭墙外气温',
  fireWaterTemperatureOut: '密闭墙外水温',
  temperatureDifferenceOut: '密闭墙外温差',
  stableDaysOut: '密闭墙外稳定天数',
};

/** 基于 DEFAULT_ELEMENT_GROUPS 预计算的操作数集合 */
export const DEFAULT_OPERAND_SET: Set<string> = buildOperandSet(DEFAULT_ELEMENT_GROUPS);
