import { genDigitComAttr } from '@/utils';
import { $t } from '@/utils/i18n';
import type { returnType, SchemaProps } from '../type';

export const exposureAlertSchema = ({
  richFormat,
}: SchemaProps): returnType => {
  return {
    popFormTips: {
      title: `📋 ${$t('form.rules.header')}`,
      dependencies: ['param1', 'param2', 'param3', 'param4'],
      content: ({ param1, param2, param3, param4 }) => {
        const isArray = Array.isArray(param1);
        const tmp = isArray ? param1 : param1?.split(',');
        const assets = `${tmp?.slice(0, 3)?.join(',')}...`;
        return richFormat?.('form.rules.header.content5_1', {
          assets: param1 ? assets : $t('pages.rules.allAssets'),
          upperThreshold: param2 || '---',
          lowerThreshold: param4 || '---',
          interval: param3 || 0,
        });
      },
    },
    leftForm: [
      {
        title: $t('pages.rules.ruleName'),
        dataIndex: 'name',
        width: '100%',
        formItemProps: {
          rules: [{ required: true, message: $t('form.required') }],
        },
      },
      {
        title: `${$t('pages.rule5_1.item2')} (USD)`,
        dataIndex: 'param2',
        valueType: 'digit',
        width: '100%',
        dependencies: ['param4'],
        formItemProps: {
          rules: [
            { required: true, message: $t('form.required') },

            ({ getFieldValue }) => ({
              validator(_, value) {
                const minValue = getFieldValue('param4');
                if (
                  value !== undefined &&
                  minValue !== undefined &&
                  value < minValue
                ) {
                  return Promise.reject(
                    new Error($t('pages.rule5_1.validateError2')),
                  );
                }
                return Promise.resolve();
              },
            }),
          ],
        },
        fieldProps: {
          ...genDigitComAttr(),
        },
      },

      {
        title: `${$t('pages.rule5_1.item4')} (USD)`,
        dataIndex: 'param4',
        valueType: 'digit',
        width: '100%',
        dependencies: ['param2'],
        formItemProps: {
          rules: [
            { required: true, message: $t('form.required') },
            ({ getFieldValue }) => ({
              validator(_, value) {
                const maxValue = getFieldValue('param2');
                if (
                  value !== undefined &&
                  maxValue !== undefined &&
                  value > maxValue
                ) {
                  return Promise.reject(
                    new Error($t('pages.rule5_1.validateError4')),
                  );
                }
                return Promise.resolve();
              },
            }),
          ],
        },
        fieldProps: {
          ...genDigitComAttr(),
          min: -Number.MAX_SAFE_INTEGER,
        },
      },
      {
        title: `${$t('pages.rules.reportInterval')} (${$t('pages.rules.seconds')})`,
        dataIndex: 'param3',
        valueType: 'digit',
        width: '100%',
        formItemProps: {
          rules: [{ required: true, message: $t('form.required') }],
        },
        fieldProps: {
          ...genDigitComAttr(0),
        },
      },
      {
        title: $t('pages.rule5_1.item5'),
        dataIndex: 'param5',
        width: '100%',
        formItemProps: {
          rules: [{ required: true, message: $t('form.required') }],
          hidden: true,
        },
      },
      {
        title: '',
        dataIndex: 'id',
        formItemProps: {
          hidden: true,
        },
      },
    ],
    transformInitialValue(values) {
      if (values.id) return values;
      return {
        ...values,
        param2: 10000000,
        param4: -10000000,
        param3: 10,
        param5: 'USD',
      };
    },
    tipsComponent: $t('form.rule5.tips'),
    assetFormShow: true,
  };
};
