import { getNeConfigData } from '@/api/ne/neConfig';
import { regExpIPv4, regExpIPv6, validURL } from '@/utils/regular-utils';
import { ref } from 'vue';

/**
 * 参数公共函数
 * @param param 父级传入 {t}
 * @returns
 */
export default function useOptions({ t }: any) {
  /**规则校验 */
  function ruleVerification(row: Record<string, any>): (string | boolean)[] {
    let result = [true, ''];
    const type = row.type;
    const value = row.value;
    const filter = row.filter;
    const display = row.display;

    // 子嵌套的不检查
    if (row.array) {
      return result;
    }

    // 可选的同时没有值不检查
    if (row.optional === 'true' && !value) {
      return result;
    }
    switch (type) {
      case 'int':
        // filter: "0~128"

        if (filter && filter.indexOf('~') !== -1) {
          const filterArr = filter.split('~');
          const minInt = parseInt(filterArr[0]);
          const maxInt = parseInt(filterArr[1]);
          const valueInt = parseInt(value);
          if (valueInt < minInt || valueInt > maxInt) {
            return [
              false,
              t('views.ne.neConfig.requireInt', {
                display,
                filter,
              }),
            ];
          }
        }
        break;
      case 'ipv4':
        if (!regExpIPv4.test(value)) {
          return [
            false,
            t('views.ne.neConfig.requireIpv4', { display }),
          ];
        }
        break;
      case 'ipv6':
        if (!regExpIPv6.test(value)) {
          return [
            false,
            t('views.ne.neConfig.requireIpv6', { display }),
          ];
        }
        break;
      case 'enum':
        if (filter && filter.indexOf('{') === 1) {
          let filterJson: Record<string, any> = {};
          try {
            filterJson = JSON.parse(filter); //string---json
          } catch (error) {
            console.error(error);
          }

          if (!Object.keys(filterJson).includes(`${value}`)) {
            return [
              false,
              t('views.ne.neConfig.requireEnum', { display }),
            ];
          }
        }
        break;
      case 'bool':
        // filter: '{"0":"false", "1":"true"}'

        if (filter && filter.indexOf('{') === 1) {
          let filterJson: Record<string, any> = {};
          try {
            filterJson = JSON.parse(filter); //string---json
          } catch (error) {
            console.error(error);
          }

          if (!Object.values(filterJson).includes(`${value}`)) {
            return [
              false,
              t('views.ne.neConfig.requireBool', { display }),
            ];
          }
        }
        break;
      case 'string':
        // filter: "0~128"

        // 字符串长度判断
        if (filter && filter.indexOf('~') !== -1) {
          try {
            const filterArr = filter.split('~');
            let rule = new RegExp(
              '^\\S{' + filterArr[0] + ',' + filterArr[1] + '}$'
            );
            if (!rule.test(value)) {
              return [
                false,
                t('views.ne.neConfig.requireString', {
                  display,
                }),
              ];
            }
          } catch (error) {
            console.error(error);
          }
        }
        // 字符串http判断
        if (value.startsWith('http')) {
          try {
            if (!validURL(value)) {
              return [
                false,
                t('views.ne.neConfig.requireString', {
                  display,
                }),
              ];
            }
          } catch (error) {
            console.error(error);
          }
        }

        break;
      case 'regex':
        // filter: "^[0-9]{3}$"

        if (filter) {
          try {
            let regex = new RegExp(filter);
            if (!regex.test(value)) {
              return [
                false,
                t('views.ne.neConfig.requireString', {
                  display,
                }),
              ];
            }
          } catch (error) {
            console.error(error);
          }
        }
        break;

      default:
        return [
          false,
          t('views.ne.neConfig.requireUn', { display }),
        ];
    }
    return result;
  }

  /**upfId可选择 */
  const smfByUPFIdOptions = ref<{ value: string; label: string }[]>([]);
  /**加载smf配置的upfId */
  function smfByUPFIdLoadData(neId: string) {
    getNeConfigData({
      neType: 'SMF',
      neId: neId,
      paramName: 'upfConfig',
    }).then(res => {
      smfByUPFIdOptions.value = [];
      for (const s of res.data) {
        smfByUPFIdOptions.value.push({
          value: s.id,
          label: s.id,
        });
      }
    });
  }

  return {
    ruleVerification,
    smfByUPFIdLoadData,
    smfByUPFIdOptions,
  };
}
