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/eftposPay/device.tsx
var device_exports = {};
__export(device_exports, {
  default: () => device_default
});
module.exports = __toCommonJS(device_exports);
var import_utils = require("@pisell/utils");
var import_ahooks = require("ahooks");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_titlebar = __toESM(require("./component/header/titlebar"));
var import_const = require("./const");
var import_helper = require("./helper");
var import_ChevronRight = __toESM(require("@pisell/icon/es/ChevronRight"));
var import_store = require("./store");
var import_manufacturer = require("./manufacturer");
var import_device = require("./device.less");
var device_default = ({
  api,
  onChange,
  onClose,
  className = "",
  device_number,
  isMobile,
  net,
  client,
  formatAmount = (number, precision, symbol) => `${symbol}${number}`,
  containerStyle = {}
}) => {
  const store = (0, import_store.useContextData)();
  const { device, symbol, mode, eftpos } = store;
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [dataStaus, setDataStaus] = (0, import_react.useState)(true);
  const [list, setList] = (0, import_react.useState)([]);
  const dispatch = (0, import_store.useDispatch)();
  const onClick = (event) => {
    if (!net) {
      import_antd.message.open({
        key: "net-warning",
        type: "warning",
        content: import_utils.locales.getText("page.eftpos-pay.text.not.connected")
      });
      return;
    }
    const [index] = [Number(event.currentTarget.dataset.index)];
    setDevice(list[index]);
  };
  (0, import_ahooks.useMount)(() => {
    setLoading(true);
    api.getDeviceList({ type: eftpos, status: "paired" }).then(
      (res) => {
        setLoading(false);
        if (res && Array.isArray(res.list) && res.list.length > 0) {
          if (device_number) {
            const hostDeviceList = res.list.filter(
              (item) => {
                var _a, _b;
                return (_b = (_a = item == null ? void 0 : item.setting) == null ? void 0 : _a.devices) == null ? void 0 : _b.some(
                  (i) => i.number === device_number
                );
              }
            );
            if (hostDeviceList.length >= 1) {
              setDataStaus(true);
              setList(setDeviceList(hostDeviceList));
              if (hostDeviceList.length === 1) {
                setDevice(hostDeviceList[0]);
              }
              return;
            }
          }
          setDataStaus(true);
          setList(setDeviceList(res.list));
          if (res.list.length === 1) {
            setDevice(res.list[0]);
          }
          return;
        }
        setDataStaus(false);
      },
      () => {
        setDataStaus(false);
        setLoading(false);
      }
    );
  });
  const setDevice = (device2) => {
    dispatch((0, import_store.updateDevice)({ device: device2 }));
    if (typeof api.selectDevice === "function") {
      api.selectDevice(device2);
    }
    if (typeof onChange === "function") {
      if (mode === import_const.ModeEnum.Pay) {
        if (device2.pay) {
          dispatch(
            (0, import_store.updateState)({
              pay: device2.pay
            })
          );
        } else {
          const amonunt = (0, import_manufacturer.getAmonunt)({ ...store, device: device2 });
          const pay = (0, import_helper.getPayAmonunt)(
            amonunt,
            (total) => formatAmount(total, 2, symbol)
          );
          dispatch((0, import_store.updateState)({ pay }));
        }
      }
      onChange("page", mode === import_const.ModeEnum.FullPay ? "amount" : "pay");
    }
  };
  const setDeviceList = (list2) => {
    return list2.map((item) => {
      const amonunt = (0, import_manufacturer.getAmonunt)({ ...store, device: item });
      const pay = (0, import_helper.getPayAmonunt)(
        amonunt,
        (total) => formatAmount(total, 2, symbol)
      );
      return { ...item, pay };
    });
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `pisell-lowcode__eft-pay-device-box ${className}` }, isMobile ? null : /* @__PURE__ */ import_react.default.createElement(import_titlebar.default, { type: "left-title-close", onClose }, dataStaus ? import_utils.locales.getText("page.eftpos-pay.text.device") : ""), dataStaus ? /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-lowcode__eft-pay-device-list",
      style: {
        ...isMobile ? { marginTop: "20px" } : {},
        ...containerStyle || {}
      }
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { spinning: loading, style: { flex: 1, overflow: "auto" } }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: { display: "flex", flexDirection: "column", gap: "10px" }
      },
      list.map(({ id, name, pay }, index) => /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key: id,
          className: `pisell-lowcode__eft-pay-device ${id === (device == null ? void 0 : device.id) ? "active" : ""}`,
          "data-index": index,
          onClick,
          role: "button",
          tabIndex: index
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-name" }, name), mode === import_const.ModeEnum.Refund ? null : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-money" }, pay.totalUnit), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-sucharge" }, import_utils.locales.getText(
          "page.eftpos-pay.text.surcharge.edit.inc.surcharge"
        ), ": ", formatAmount(`${pay.surcharge}`, 2, symbol), " (", import_utils.locales.getText(
          "page.eftpos-pay.text.surcharge.edit.rate"
        ), ":", pay.surRate, "% +", " ", formatAmount(`${pay.surMoney}`, 2, symbol), ")"))),
        /* @__PURE__ */ import_react.default.createElement("div", { style: { flexShrink: 0 } }, /* @__PURE__ */ import_react.default.createElement(
          import_ChevronRight.default,
          {
            style: { fontWeight: "600", fontSize: "24px" }
          }
        ))
      ))
    ))
  ) : /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-lowcode__eft-pay-device-error",
      style: isMobile ? { marginTop: "20px" } : {}
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-error-tit" }, import_utils.locales.getText("page.eftpos-pay.text.device.no.title")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-error-txt" }, import_utils.locales.getText("page.eftpos-pay.text.device.no.add")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-error-txt" }, import_utils.locales.getText("page.eftpos-pay.text.device.no.web")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-device-error-txt" }, import_utils.locales.getText("page.eftpos-pay.text.device.no.terminal"))
  ));
};
