import { Rule } from 'antd/es/form';

export const rulesHelper = {
  text: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          return value && value !== ''
            ? Promise.resolve()
            : Promise.reject(new Error('不能为空'));
        },
      }),
    ] as Rule[],
  },

  phone: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          let phoneRegex = /^1[3-9]\d{9}$/;
          if (!value) {
            return Promise.reject(new Error('请输入手机号'));
          }
          if (value?.length !== 11) {
            return Promise.reject(new Error('手机号至少11位'));
          }
          if (!phoneRegex.test(value)) {
            return Promise.reject(new Error('请输入正确的手机号'));
          }
          return Promise.resolve();
        },
      }),
    ] as Rule[],
  },
  upload(props?: { max?: number; errMsg?: string }) {
    return {
      required: true,
      rules: [
        () => ({
          validator(_, value) {
            if (!value) return Promise.reject(new Error('请上传'));
            let doneUploads = value?.filter(
              (item: { status: string }) => item.status === 'done',
            );
            if (!doneUploads) return Promise.reject(new Error('请上传'));

            return doneUploads.length > (props?.max ?? 1)
              ? Promise.reject(new Error(props?.errMsg ?? '请上传'))
              : Promise.resolve();
          },
        }),
      ] as Rule[],
    };
  },
  number: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          if (value !== undefined && value !== '' && !isNaN(Number(value))) {
            return Promise.resolve();
          } else {
            return Promise.reject(new Error('请输入数字，且不能为空'));
          }
        },
      }),
    ] as Rule[],
  },
  boolean: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          return value !== undefined && value !== ''
            ? Promise.resolve()
            : Promise.reject(new Error('不能为空'));
        },
      }),
    ] as Rule[],
  },
  richtext: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          if (value && value !== '<p><br></p>') {
            return Promise.resolve();
          } else {
            return Promise.reject(new Error('不能为空'));
          }
        },
      }),
    ] as Rule[],
  },
  array: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          if (value && value.length > 0) {
            return Promise.resolve();
          } else {
            return Promise.reject(new Error('不能为空'));
          }
        },
      }),
    ] as Rule[],
  },
  cascader: {
    required: true,
    rules: [
      () => ({
        validator(_, value) {
          if (value && value.length > 0) {
            value.forEach((item: any) => {
              if (item === undefined) {
                return Promise.reject(new Error('不能为空'));
              }
            });
            return Promise.resolve();
          } else {
            return Promise.reject(new Error('不能为空'));
          }
        },
      }),
    ] as Rule[],
  },

  getMonthStartDate(date: string) {
    //获取当月的开始日期
    if (date) {
      const targetDate = new Date(date);
      // 设置为当月第一天
      targetDate.setDate(1);
      return targetDate.toISOString().split('T')[0];
    } else {
      return undefined;
    }
  },
  getMonthEndDate(date: string) {
    //获取当月的结束日期
    if (date) {
      const targetDate = new Date(date);
      // 设置为下个月第一天，然后减去一天得到当月最后一天
      targetDate.setMonth(targetDate.getMonth() + 1, 1);
      targetDate.setDate(targetDate.getDate() - 1);
      return targetDate.toISOString().split('T')[0];
    } else {
      return undefined;
    }
  },
  getDifference(startNum: number, endNum: number) {
    //获取2 个数据之间的差值  单位：天
    if (startNum && endNum) {
      const start = startNum;
      const end = endNum;
      const diffDays = end - start;
      return diffDays;
    } else {
      return undefined;
    }
  },
};
