function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
import React, { useMemo, forwardRef } from 'react';
import { Col, Form, Select } from 'antd';
import "./index.less";

// const data = {
//   type: 'product',
//   custom: [
//     {
//       id: 1,
//       name: {
//         en: '成人',
//         'zh-CN': '成人',
//         'zh-HK': '成人',
//         original: '成人',
//       },
//       min: 1,
//       max: 10,
//     },
//     {
//       id: 2,
//       name: {
//         en: '成人',
//         'zh-CN': '成人',
//         'zh-HK': '成人',
//         original: '儿童',
//       },
//       min: 1,
//       max: 10,
//     },
//   ],
//   package: {
//     product_ids: [],
//   },
// };

var getName = function getName(name, locale) {
  return name[locale] || name['original'] || name['en'] || '';
};
var getSelectOptions = function getSelectOptions(min, max) {
  var arr = [];
  for (var i = min; i <= max; i++) {
    arr.push({
      label: "".concat(i),
      value: i
    });
  }
  return arr;
};
var Capacity = function Capacity(_ref, ref) {
  var _cacheItem$_data;
  var state = _ref.state,
    cacheItem = _ref.cacheItem,
    value = _ref.value,
    onChange = _ref.onChange;
  var data = cacheItem === null || cacheItem === void 0 || (_cacheItem$_data = cacheItem._data) === null || _cacheItem$_data === void 0 ? void 0 : _cacheItem$_data.capacitys;
  var _value = useMemo(function () {
    return value || [];
  }, [value]);
  var _onChange = function _onChange(val, index) {
    var _val = _toConsumableArray(_value);
    if (!_val.find(function (item) {
      return item.id === index;
    })) {
      var _data$custom$find;
      (_data$custom$find = data.custom.find(function (item) {
        return item.id === index;
      })) === null || _data$custom$find === void 0 || _data$custom$find.min;
      _val.push({
        id: index,
        value: val
      });
      onChange === null || onChange === void 0 || onChange(_val);
      return;
    }
    _val = _val.map(function (item) {
      if (item.id === index) {
        return _objectSpread(_objectSpread({}, item), {}, {
          value: val
        });
      }
      return item;
    });
    console.log(_val);
    onChange === null || onChange === void 0 || onChange(_val);
  };
  if ((data === null || data === void 0 ? void 0 : data.type) !== 'custom') {
    return null;
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, data.custom.map(function (item) {
    var _value$find;
    return /*#__PURE__*/React.createElement(Col, {
      span: 24,
      key: item.id
    }, /*#__PURE__*/React.createElement(Form.Item, {
      label: getName(item.name, state.locale),
      required: true
    }, /*#__PURE__*/React.createElement(Select, {
      size: "large",
      className: "capacity-select",
      onChange: function onChange(e) {
        _onChange(e, item.id);
      },
      showSearch: !state.isTerminal,
      value: (_value$find = _value.find(function (d) {
        return d.id === item.id;
      })) === null || _value$find === void 0 ? void 0 : _value$find.value,
      options: getSelectOptions(item.min, item.max),
      onSearch: function onSearch(e) {
        if (!e) {
          return;
        }
        var num = Number(e);
        if (isNaN(num)) {
          return;
        }
        if (num >= item.min && num <= item.max) {
          _onChange(num, item.id);
        }
      }
    })));
  }));
};
export default /*#__PURE__*/forwardRef(Capacity);