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 _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 _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, { forwardRef, useImperativeHandle, useState, useMemo } from 'react';
import { PisellModal } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { formatService } from "../../utils";
import { Select } from 'antd';
import "./index.less";
var SelectPetModal = function SelectPetModal(_ref, ref) {
  var onChange = _ref.onChange;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    params = _useState4[0],
    setParams = _useState4[1];
  useImperativeHandle(ref, function () {
    return {
      open: function open(_ref2) {
        var _holders$;
        var products = _ref2.products,
          holders = _ref2.holders;
        var _holders = holders === null || holders === void 0 ? void 0 : holders.filter(function (item) {
          return item.key > 0;
        });
        // 将service默认的holder_id设置为当前第一个宠物的id
        var service = formatService(products, _holders === null || _holders === void 0 || (_holders$ = _holders[0]) === null || _holders$ === void 0 ? void 0 : _holders$.key);
        setParams({
          service: service,
          holders: _holders
        });
        setOpen(true);
      }
    };
  });
  var handleChange = function handleChange(e, index) {
    var _lists = _toConsumableArray(params === null || params === void 0 ? void 0 : params.service);
    _lists[index].holder_id = e;
    console.log(e, index, _lists, 'wh79876');
    setParams(_objectSpread(_objectSpread({}, params), {}, {
      service: _lists
    }));
  };

  /**
   * @title: 弹窗内容
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-12-03 16:26
   */
  var content = useMemo(function () {
    var _params$holders;
    var services = (params === null || params === void 0 ? void 0 : params.service) || [];
    var options = params === null || params === void 0 || (_params$holders = params.holders) === null || _params$holders === void 0 ? void 0 : _params$holders.map(function (item) {
      return {
        value: item.key,
        label: item.main_field
      };
    });
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__booking-select-pet-content"
    }, services === null || services === void 0 ? void 0 : services.map(function (item, index) {
      var _options$;
      return /*#__PURE__*/React.createElement("div", {
        className: "pisell-lowcode__booking-select-pet-item",
        key: item._id
      }, /*#__PURE__*/React.createElement("span", null, " ", item.name), /*#__PURE__*/React.createElement(Select, {
        defaultValue: (_options$ = options[0]) === null || _options$ === void 0 ? void 0 : _options$.value,
        onChange: function onChange(e) {
          return handleChange(e, index);
        },
        options: options,
        popupMatchSelectWidth: false
      }));
    }));
  }, [params === null || params === void 0 ? void 0 : params.service, params === null || params === void 0 ? void 0 : params.pets]);
  var handleCancel = function handleCancel() {
    setOpen(false);
  };

  /**
   * @title: 点击确定
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-12-03 17:05
   */
  var handleOk = function handleOk() {
    var service = params.service;
    var holders = service === null || service === void 0 ? void 0 : service.map(function (item) {
      return {
        _id: item._id,
        holder_id: item.holder_id
      };
    });
    onChange(service, 'update_holders', holders);
    handleCancel();
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(PisellModal, {
    platform: 'pc',
    open: open,
    bodyStyle: {
      padding: 0
    },
    title: locales.getText('pisell2.text.shopping-cart.select.holder.modal.title'),
    okText: locales.getText('pisell2.text.shopping-cart.confirm'),
    cancelText: locales.getText('pisell2.text.shopping-cart.cancel'),
    onCancel: handleCancel,
    onOk: handleOk,
    destroyOnClose: true,
    className: "pisell-lowcode__booking-select-pet-modal-wrap"
  }, content));
};
export default /*#__PURE__*/forwardRef(SelectPetModal);