var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/ecocup/components/CustomerAndCupModal/index.tsx
var CustomerAndCupModal_exports = {};
__export(CustomerAndCupModal_exports, {
  default: () => CustomerAndCupModal_default
});
module.exports = __toCommonJS(CustomerAndCupModal_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_ahooks = require("ahooks");
var import_CustomerCard = __toESM(require("../CustomerCard"));
var import_SearchBar = __toESM(require("../SearchBar"));
var import_index = require("./index.less");
var import_CupActionList = __toESM(require("../CupActionList"));
var import_types = require("../../types");
var import_hooks = require("./hooks");
var SCAN_LISTENER_KEY = "eco-cup-customer-first-modal-scan-listener";
var CustomerAndCupModal = (props) => {
  const { key, title, visible, onCancel, onOk, data } = props;
  const [customerData, setCustomerData] = (0, import_react.useState)();
  const [rentedCups, setRentedCups] = (0, import_react.useState)([]);
  const [newCups, setNewCups] = (0, import_react.useState)([]);
  const [maxCupsNum, setMaxCupsNum] = (0, import_react.useState)(0);
  const customerDetail = (0, import_hooks.useCustomerDetail)();
  const batchRentCups = (0, import_hooks.useBatchRentCups)();
  const currentCupsNum = (0, import_react.useMemo)(() => {
    return ((rentedCups == null ? void 0 : rentedCups.length) || 0) + ((newCups == null ? void 0 : newCups.length) || 0);
  }, [rentedCups, newCups]);
  const isOverMax = (0, import_react.useMemo)(() => {
    return currentCupsNum > maxCupsNum;
  }, [currentCupsNum, maxCupsNum]);
  const isLoading = (0, import_react.useMemo)(() => {
    return (customerDetail == null ? void 0 : customerDetail.loading) || (batchRentCups == null ? void 0 : batchRentCups.loading);
  }, [customerDetail == null ? void 0 : customerDetail.loading, batchRentCups == null ? void 0 : batchRentCups.loading]);
  (0, import_react.useEffect)(() => {
    setCustomerData((data == null ? void 0 : data.customerData) || void 0);
  }, [data == null ? void 0 : data.customerData]);
  const _getCustomerDetail = (0, import_ahooks.useMemoizedFn)((id) => {
    if (!id) return;
    customerDetail.run(id).then((res) => {
      console.log("res", res);
      if (res) {
        setMaxCupsNum((res == null ? void 0 : res.max_rent_count) || 0);
        setRentedCups((res == null ? void 0 : res.rent_cups) || []);
      } else {
        setMaxCupsNum(0);
        setRentedCups([]);
      }
    });
  });
  (0, import_react.useEffect)(() => {
    var _a;
    if ((data == null ? void 0 : data.firstType) === "cup") {
      (data == null ? void 0 : data.cupData) && setNewCups([data == null ? void 0 : data.cupData]);
    } else {
      _getCustomerDetail((_a = data == null ? void 0 : data.customerData) == null ? void 0 : _a.id);
    }
  }, [data == null ? void 0 : data.firstType, data == null ? void 0 : data.customerData, data == null ? void 0 : data.cupData, _getCustomerDetail]);
  const _onCancel = () => {
    if (isLoading) return;
    onCancel == null ? void 0 : onCancel();
  };
  const _onOk = async () => {
    if (isLoading || !(customerData == null ? void 0 : customerData.id)) return;
    const cupIds = newCups.map((c) => c == null ? void 0 : c.id);
    const result = await batchRentCups.run(customerData == null ? void 0 : customerData.id, cupIds);
    if (!result) {
      return;
    }
    setNewCups([]);
    _getCustomerDetail(customerData == null ? void 0 : customerData.id);
  };
  const onSearchResult = (result) => {
    var _a, _b;
    console.log("onSearchResult", result);
    if ((result == null ? void 0 : result.type) === "cup") {
      if (newCups.find((c) => {
        var _a2;
        return (c == null ? void 0 : c.id) === ((_a2 = result == null ? void 0 : result.data) == null ? void 0 : _a2.id);
      })) {
        return;
      }
      if (((_a = result == null ? void 0 : result.data) == null ? void 0 : _a.status) !== import_types.ECupStatus.IN_STOCK) {
        return;
      }
      setNewCups([...newCups, result == null ? void 0 : result.data]);
    } else {
      setCustomerData(result == null ? void 0 : result.data);
      _getCustomerDetail((_b = result == null ? void 0 : result.data) == null ? void 0 : _b.id);
    }
  };
  const renderCustomerCard = () => {
    return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-title" }, "Current Customer"), (customerData == null ? void 0 : customerData.id) ? /* @__PURE__ */ import_react.default.createElement(import_CustomerCard.default, { ...customerData || {} }) : /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-no-customer" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-no-customer-icon" }), /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell2-ecocup-customer-first-modal-no-customer-text" }, import_utils.locales.getText(
      "pisell2.text.ecocup.customer-first-modal.no-customer"
    ))));
  };
  const renderSearchBar = () => {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar" }, /* @__PURE__ */ import_react.default.createElement(
      import_SearchBar.default,
      {
        scanListenerKey: `${key}-${SCAN_LISTENER_KEY}`,
        searchType: (customerData == null ? void 0 : customerData.id) ? "cup" : void 0,
        placeholder: (customerData == null ? void 0 : customerData.id) ? import_utils.locales.getText(
          "pisell2.text.ecocup.customer-first-modal.search-bar.placeholder-cup"
        ) : import_utils.locales.getText(
          "pisell2.text.ecocup.customer-first-modal.search-bar.placeholder"
        ),
        onSearchResult,
        customerId: customerData == null ? void 0 : customerData.id
      }
    ), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar-limit" }, /* @__PURE__ */ import_react.default.createElement("span", null, "Max"), /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", { style: { color: isOverMax ? "#CF1322" : "#000000" } }, currentCupsNum), "/", maxCupsNum)));
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open: visible,
      title: title || import_utils.locales.getText("pisell2.text.ecocup.customer-first-modal.title"),
      headerDivider: false,
      onCancel: _onCancel,
      onOk: _onOk,
      okText: import_utils.locales.getText("pisell2.text.ecocup.button.confirm"),
      cancelText: import_utils.locales.getText("pisell2.text.ecocup.button.dismiss"),
      okButtonProps: {
        disabled: newCups.length === 0 || isOverMax || isLoading || !(customerData == null ? void 0 : customerData.id),
        loading: isLoading
      },
      width: 580
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal" }, (data == null ? void 0 : data.firstType) === "customer" ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderCustomerCard(), renderSearchBar()) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderSearchBar(), renderCustomerCard()), /* @__PURE__ */ import_react.default.createElement(
      import_CupActionList.default,
      {
        maxNum: maxCupsNum,
        currentNum: currentCupsNum,
        cups: [...rentedCups, ...newCups],
        customerData,
        onClearAll: () => setNewCups([]),
        onRemove: (cup) => {
          setNewCups(newCups.filter((c) => (c == null ? void 0 : c.id) !== (cup == null ? void 0 : cup.id)));
        },
        onAction: (type, cup) => {
          const key2 = (cup == null ? void 0 : cup.eco_cup_id) ? "eco_cup_id" : "id";
          setRentedCups(
            rentedCups.filter((c) => (c == null ? void 0 : c[key2]) !== (cup == null ? void 0 : cup[key2]))
          );
        }
      }
    ))
  );
};
var CustomerAndCupModal_default = CustomerAndCupModal;
