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/checkout/components/SearchAndClientModule/index.tsx
var SearchAndClientModule_exports = {};
__export(SearchAndClientModule_exports, {
  default: () => SearchAndClientModule_default
});
module.exports = __toCommonJS(SearchAndClientModule_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_components = require("../../../../pro/comprehensiveSearch/components");
var import_cartClientCard = __toESM(require("../../../booking/info2/cartClientCard"));
var import_pisellos = require("../../../ticketBooking/hooks/pisellos");
var import_useToast = __toESM(require("../../../../hooks/useToast"));
var import_index = require("./index.less");
var SearchAndClientModule = (props) => {
  const { disabledSwitch, onSearch, onClientChange } = props;
  const [value, setValue] = (0, import_react.useState)("");
  const [loading, setLoading] = (0, import_react.useState)(false);
  const clientCardRef = (0, import_react.useRef)(null);
  const bookingTicket = (0, import_pisellos.useBookingTicket)();
  const handleSearch = (0, import_ahooks.useDebounceFn)(
    async (keyword) => {
      var _a, _b, _c, _d, _e;
      if (!!keyword) {
        setLoading(true);
        try {
          const res = await (onSearch == null ? void 0 : onSearch(keyword));
          if (!(res == null ? void 0 : res.data) || Array.isArray(res.data) && res.data.length === 0) {
            (_a = Toast == null ? void 0 : Toast.info) == null ? void 0 : _a.call(
              Toast,
              import_utils.locales.getText("pisell2.checkout.wallet-pass-module.no-result")
            );
            return;
          }
          if ("type" in res && res.type == "walletCode") {
            if (disabledSwitch) {
              (_b = Toast == null ? void 0 : Toast.info) == null ? void 0 : _b.call(
                Toast,
                import_utils.locales.getText(
                  "pisell2.checkout.client-card.tip.partially-paid"
                )
              );
              return;
            }
            const { customer, ...rest } = res.data;
            if (!customer) {
              (_c = Toast == null ? void 0 : Toast.info) == null ? void 0 : _c.call(
                Toast,
                import_utils.locales.getText("pisell2.checkout.wallet-pass-module.no-result")
              );
              return;
            }
            let _customer = {
              ...customer,
              latest_wallet_detail: rest
            };
            (_e = (_d = clientCardRef.current) == null ? void 0 : _d.handleSetClient) == null ? void 0 : _e.call(_d, _customer, {
              sameClientTip: import_utils.locales.getText(
                "pisell2.checkout.client-card.tip.same-client"
              )
            });
          }
          setValue("");
        } catch (error) {
        } finally {
          setLoading(false);
        }
      }
    },
    { wait: 300, leading: true }
  );
  const uniqueId = () => {
    const timestamp = Date.now();
    return timestamp.toString().slice(-4);
  };
  (0, import_react.useEffect)(() => {
    const actions = bookingTicket == null ? void 0 : bookingTicket.scanUniversalListener(
      (data) => {
        console.log("handle scan universal resp", data);
        if (data.status === "failed") {
          Toast.fail(import_utils.locales.getText("pisell2.text.scan-global-failed"));
          return;
        }
        if (data.scanCode) {
          setValue(data.scanCode);
          setTimeout(() => {
            handleSearch.run(data.scanCode);
          });
        }
      },
      `wallet_scan_${uniqueId()}`
    );
    return () => {
      var _a;
      return (_a = actions == null ? void 0 : actions.remove) == null ? void 0 : _a.call(actions);
    };
  }, [bookingTicket, handleSearch]);
  const Toast = (0, import_useToast.default)();
  const dropdownProps = (0, import_react.useMemo)(() => {
    const icon = /* @__PURE__ */ import_react.default.createElement(
      import_materials.Iconfont,
      {
        type: "pisell2-switch-horizontal-01",
        style: { fontSize: 24, color: "#7F56DA" }
      }
    );
    let _props = {
      buttonProps: {
        icon
      }
    };
    if (disabledSwitch === true) {
      _props = {
        open: false,
        buttonProps: {
          icon,
          onClick: () => {
            var _a;
            (_a = Toast == null ? void 0 : Toast.info) == null ? void 0 : _a.call(
              Toast,
              import_utils.locales.getText("pisell2.checkout.client-card.tip.partially-paid")
            );
          },
          style: { cursor: "not-allowed" }
        }
      };
    }
    return _props;
  }, [Toast, disabledSwitch]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        display: "flex",
        gap: 8,
        marginBottom: 8
      }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_components.SearchSection,
      {
        value,
        onChange: setValue,
        loading,
        style: { flex: 1 },
        placeholder: import_utils.locales.getText(
          "pisell2.checkout.search-section.placeholder"
        ),
        searchButtonProps: {
          placement: "inside",
          style: {
            height: 40,
            padding: 8,
            minWidth: 55
          }
        },
        cameraButtonProps: {
          icon: ["pisell2-camera-01", "pisell2-a-qr-code-02"]
        },
        onSearch: () => handleSearch.run(value)
      }
    ),
    /* @__PURE__ */ import_react.default.createElement(
      import_cartClientCard.default,
      {
        ref: clientCardRef,
        onClientChange: (client) => {
          onClientChange == null ? void 0 : onClientChange();
        },
        clientCardProps: {
          style: {
            width: 240,
            minHeight: 60,
            height: 60,
            borderColor: "#7F56DA"
          },
          avatar: {
            size: "md"
          },
          dropdown: dropdownProps
        }
      }
    )
  );
};
var SearchAndClientModule_default = SearchAndClientModule;
