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, { useImperativeHandle, useState, forwardRef, useRef, useCallback, useMemo } from 'react';
import { PisellModal, Button, Tag, Icon } from '@pisell/materials';
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import localeTexts from "./locales";
import { request } from "../../utils";
import "./index.less";
/** 收起时列表最多展示的条目数 */
var MAX_COLLAPSED_ITEMS = 5;
var ProductStockCheckModal = function ProductStockCheckModal(props, ref) {
  var _context$engine, _context$appHelper, _context$appHelper2, _context$appHelper2$i;
  var context = useEngineContext();
  locales.init(localeTexts, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || 'en');
  request.setRequest(context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request);
  var isTerminal = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$i = _context$appHelper2.isTerminal) === null || _context$appHelper2$i === void 0 ? void 0 : _context$appHelper2$i.call(_context$appHelper2);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var modalParamsRef = useRef();
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    items = _useState4[0],
    setItems = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    isAllItemsSoldOut = _useState6[0],
    setIsAllItemsSoldOut = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    listExpanded = _useState8[0],
    setListExpanded = _useState8[1];
  var onOpen = useCallback(function (params) {
    var _params$unavailablePr, _params$isAllItemsSol, _props$onOpen;
    modalParamsRef.current = params;
    setItems((_params$unavailablePr = params === null || params === void 0 ? void 0 : params.unavailableProducts) !== null && _params$unavailablePr !== void 0 ? _params$unavailablePr : []);
    setIsAllItemsSoldOut((_params$isAllItemsSol = params === null || params === void 0 ? void 0 : params.isAllItemsSoldOut) !== null && _params$isAllItemsSol !== void 0 ? _params$isAllItemsSol : false);
    setListExpanded(false);
    setVisible(true);
    (_props$onOpen = props.onOpen) === null || _props$onOpen === void 0 || _props$onOpen.call(props);
  }, [props]);
  var onClose = useCallback(function () {
    var _props$onClose;
    modalParamsRef.current = undefined;
    setItems([]);
    setIsAllItemsSoldOut(false);
    setVisible(false);
    (_props$onClose = props.onClose) === null || _props$onClose === void 0 || _props$onClose.call(props);
  }, []);
  useImperativeHandle(ref, function () {
    return {
      open: onOpen,
      close: onClose
    };
  });
  var handleReturn = useCallback(function () {
    var _props$onReturn;
    onClose();
    (_props$onReturn = props.onReturn) === null || _props$onReturn === void 0 || _props$onReturn.call(props, items);
  }, [props.onReturn, onClose, items]);
  var handleContinue = useCallback(function () {
    onClose();
    if (isAllItemsSoldOut) {
      handleReturn();
    } else {
      var _props$onContinue;
      (_props$onContinue = props.onContinue) === null || _props$onContinue === void 0 || _props$onContinue.call(props, items);
    }
  }, [props.onContinue, onClose, isAllItemsSoldOut, items]);
  var showListExpandToggle = useMemo(function () {
    return items.length > MAX_COLLAPSED_ITEMS;
  }, [items.length]);
  var displayItems = useMemo(function () {
    if (!showListExpandToggle || listExpanded) {
      return items;
    }
    return items.slice(0, MAX_COLLAPSED_ITEMS);
  }, [items, listExpanded, showListExpandToggle]);
  return /*#__PURE__*/React.createElement(PisellModal, {
    platform: isTerminal ? 'h5' : undefined,
    visible: visible,
    header: false,
    footer: null,
    maskClosable: false,
    destroyOnClose: true,
    closable: false,
    onClose: onClose,
    onCancel: onClose,
    wrapClassName: "product-stock-check-modal",
    style: {
      zIndex: 1001
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "product-stock-check-modal-inner"
  }, /*#__PURE__*/React.createElement("div", {
    className: "product-stock-check-modal-icon",
    "aria-hidden": true
  }, /*#__PURE__*/React.createElement("span", {
    className: "product-stock-check-modal-icon-mark"
  }, "!")), /*#__PURE__*/React.createElement("h2", {
    className: "product-stock-check-modal-title"
  }, isAllItemsSoldOut ? locales.getText('pisell2.product.stock.check.modal.all_items_title') : locales.getText('pisell2.product.stock.check.modal.title')), /*#__PURE__*/React.createElement("p", {
    className: "product-stock-check-modal-subtitle"
  }, locales.getText('pisell2.product.stock.check.modal.subtitle')), /*#__PURE__*/React.createElement("div", {
    className: "product-stock-check-modal-list"
  }, /*#__PURE__*/React.createElement("div", {
    className: "product-stock-check-modal-list-items"
  }, displayItems.map(function (item, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: "product-stock-check-modal-item"
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-stock-check-modal-item-text"
    }, /*#__PURE__*/React.createElement("span", {
      className: "product-stock-check-modal-item-title"
    }, item.title), /*#__PURE__*/React.createElement("span", {
      className: "product-stock-check-modal-item-qty"
    }, locales.getText('pisell2.product.stock.check.modal.item.quantity.prefix'), ' ', "x", item.quantity)), /*#__PURE__*/React.createElement("span", {
      className: "product-stock-check-modal-tag"
    }, /*#__PURE__*/React.createElement(Tag, {
      color: "error"
    }, locales.getText('pisell2.product.stock.check.modal.status.sold_out'))));
  })), showListExpandToggle && /*#__PURE__*/React.createElement("div", {
    className: "product-stock-check-modal-list-toggle",
    onClick: function onClick() {
      return setListExpanded(function (v) {
        return !v;
      });
    }
  }, listExpanded ? locales.getText('pisell2.product.stock.check.modal.btn.collapse') : locales.getText('pisell2.product.stock.check.modal.btn.expand'), /*#__PURE__*/React.createElement(Icon, {
    type: listExpanded ? 'pisell2-chevron-up-double' : 'pisell2-chevron-down-double'
  }))), /*#__PURE__*/React.createElement("div", {
    className: "product-stock-check-modal-buttons"
  }, /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    onClick: handleContinue
  }, isAllItemsSoldOut ? locales.getText('pisell2.product.stock.check.modal.btn.remove_all_continue') : locales.getText('pisell2.product.stock.check.modal.btn.remove_continue')), /*#__PURE__*/React.createElement(Button, {
    type: "text",
    onClick: handleReturn
  }, locales.getText('pisell2.product.stock.check.modal.btn.go_back')))));
};
export default /*#__PURE__*/forwardRef(ProductStockCheckModal);