function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useEffect, useMemo } from 'react';
import { Select, DatePicker, Form } from 'antd';
import dayjs from 'dayjs';
import useEngineContext from "../../../../hooks/useEngineContext";
import localeTexts from "../../locales";
import { locales } from '@pisell/utils';
var Option = Select.Option;

// 有效期配置类型 - 扩展原有的ValidForConfig

// 预定义的有效期选项值
var DEFAULT_VALIDITY_OPTIONS = [{
  unit: '1',
  unit_type: 'day',
  type: 'normal'
}, {
  unit: '14',
  unit_type: 'day',
  type: 'normal'
}, {
  unit: '1',
  unit_type: 'month',
  type: 'normal'
}, {
  unit: '2',
  unit_type: 'month',
  type: 'normal'
}, {
  unit: '3',
  unit_type: 'month',
  type: 'normal'
}, {
  unit: '4',
  unit_type: 'month',
  type: 'normal'
}, {
  unit: '5',
  unit_type: 'month',
  type: 'normal'
}, {
  unit: '6',
  unit_type: 'month',
  type: 'normal'
}, {
  unit: '1',
  unit_type: 'year',
  type: 'normal'
}, {
  unit: '3',
  unit_type: 'year',
  type: 'normal'
}, {
  type: 'forever'
}, {
  custom_date: '',
  type: 'custom'
}];

// 将选项值转换为显示标签的函数
var getOptionLabel = function getOptionLabel(option) {
  if (option.type === 'forever') {
    return locales.getText('forever');
  }
  if (option.type === 'custom') {
    return locales.getText('custom');
  }
  var unitMap = {
    day: locales.getText('day'),
    month: locales.getText('month'),
    year: locales.getText('year'),
    days: locales.getText('days'),
    months: locales.getText('months'),
    years: locales.getText('years')
  };
  var unit = option.unit,
    unit_type = option.unit_type;
  if (Number(unit) > 1) {
    return "".concat(unit, " ").concat(unitMap[unit_type + 's']);
  }
  return "".concat(unit, " ").concat(unitMap[unit_type]);
};

// 将ValidityPeriodValue对象转换为字符串值
var transformValueToString = function transformValueToString(option) {
  if (option.type === 'forever') {
    return 'forever';
  }
  if (option.type === 'custom') {
    return 'custom';
  }
  // normal类型：unit值 + unit_type的形式
  return "".concat(option.unit).concat(option.unit_type);
};

// 将字符串值转换回ValidityPeriodValue对象
var transformStringToValue = function transformStringToValue(stringValue) {
  if (stringValue === 'forever') {
    return {
      type: 'forever'
    };
  }
  if (stringValue === 'custom') {
    return {
      type: 'custom',
      custom_date: ''
    };
  }

  // 解析normal类型的值，例如 "1day", "2month", "3year"
  var match = stringValue.match(/^(\d+)(day|month|year)$/);
  if (match) {
    var _match = _slicedToArray(match, 3),
      unit = _match[1],
      unit_type = _match[2];
    return {
      type: 'normal',
      unit: unit,
      unit_type: unit_type
    };
  }

  // 默认返回值
  return {
    type: 'normal',
    unit: '1',
    unit_type: 'day'
  };
};
var ValidityPeriodSelector = function ValidityPeriodSelector(_ref) {
  var _context$appHelper$ut;
  var value = _ref.value,
    onChange = _ref.onChange,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    placeholder = _ref.placeholder,
    _ref$options = _ref.options,
    options = _ref$options === void 0 ? DEFAULT_VALIDITY_OPTIONS : _ref$options;
  var context = useEngineContext();
  locales.init(localeTexts, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get('umi_locale')) || 'en');
  var defaultPlaceholder = placeholder || locales.getText('selectValidityPeriod');
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showCustomDate = _useState2[0],
    setShowCustomDate = _useState2[1];
  var _useState3 = useState(value),
    _useState4 = _slicedToArray(_useState3, 2),
    currentValue = _useState4[0],
    setCurrentValue = _useState4[1];

  // 使用useMemo转换选项值为带标签的选项
  var validityOptions = useMemo(function () {
    return options.map(function (option) {
      return {
        label: getOptionLabel(option),
        value: transformValueToString(option)
      };
    });
  }, [options]);
  useEffect(function () {
    setCurrentValue(value);
    setShowCustomDate((value === null || value === void 0 ? void 0 : value.type) === 'custom');
  }, [value]);
  var handleSelectChange = function handleSelectChange(selectedValue) {
    var parsedValue = transformStringToValue(selectedValue);
    setCurrentValue(parsedValue);
    setShowCustomDate(parsedValue.type === 'custom');
    if (parsedValue.type !== 'custom') {
      onChange === null || onChange === void 0 || onChange(parsedValue);
    }
  };
  var handleDateChange = function handleDateChange(date, dateString) {
    if (date && (currentValue === null || currentValue === void 0 ? void 0 : currentValue.type) === 'custom') {
      var newValue = {
        type: 'custom',
        custom_date: date.format('YYYY-MM-DD')
      };
      setCurrentValue(newValue);
      onChange === null || onChange === void 0 || onChange(newValue);
    }
  };
  var getSelectValue = function getSelectValue() {
    if (!currentValue) return undefined;
    return transformValueToString(currentValue);
  };
  var renderOptions = function renderOptions() {
    return validityOptions.map(function (option) {
      return /*#__PURE__*/React.createElement(Option, {
        key: option.label,
        value: option.value
      }, option.label);
    });
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select, {
    placeholder: defaultPlaceholder,
    onChange: handleSelectChange,
    value: getSelectValue(),
    disabled: disabled,
    style: {
      width: '100%'
    }
  }, renderOptions()), showCustomDate && /*#__PURE__*/React.createElement(Form.Item, {
    label: locales.getText('custom'),
    style: {
      marginBottom: 0
    }
  }, /*#__PURE__*/React.createElement(DatePicker, {
    placeholder: locales.getText('selectCustomDate'),
    onChange: handleDateChange,
    value: (currentValue === null || currentValue === void 0 ? void 0 : currentValue.type) === 'custom' && currentValue.custom_date ? dayjs(currentValue.custom_date) : undefined,
    disabled: disabled,
    style: {
      width: '100%'
    }
  })));
};
export default ValidityPeriodSelector;