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/plus/saasDevice/appPeripheral/components/PeripheralConnectionCardContainer.tsx
var PeripheralConnectionCardContainer_exports = {};
__export(PeripheralConnectionCardContainer_exports, {
  default: () => PeripheralConnectionCardContainer_default
});
module.exports = __toCommonJS(PeripheralConnectionCardContainer_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_usePeripheralRuntime = require("../hooks/usePeripheralRuntime");
var import_usePeripheralLogger = require("../hooks/usePeripheralLogger");
var import_peripheralCardModel = require("../utils/peripheralCardModel");
var import_PeripheralActionModal = __toESM(require("./PeripheralActionModal"));
var import_PeripheralConnectionCard = __toESM(require("./PeripheralConnectionCard"));
var import_PhysicalDeviceSelectModal = __toESM(require("./PhysicalDeviceSelectModal"));
var PeripheralConnectionCardContainer = ({
  row,
  variant,
  getText,
  translation,
  getAppInstance,
  onPhysicalChange,
  onUsageChange,
  onTest,
  onEdit,
  onRemove,
  onDeleteDevice
}) => {
  const { addLog: addActionLog } = (0, import_usePeripheralLogger.usePeripheralLogger)();
  const [actionsOpen, setActionsOpen] = (0, import_react.useState)(false);
  const [ignoreConfirmOpen, setIgnoreConfirmOpen] = (0, import_react.useState)(false);
  const [removing, setRemoving] = (0, import_react.useState)(false);
  const [testLoading, setTestLoading] = (0, import_react.useState)(false);
  const {
    runtimeState,
    physicalSelect,
    isRuntimeSubmitting,
    setPhysicalSelect,
    handleConnectPeripheral,
    handleRemovePeripheral,
    handleUpdatePeripheralUsage,
    connectSelectedPhysicalDevice
  } = (0, import_usePeripheralRuntime.usePeripheralRuntime)({
    row,
    getAppInstance,
    getText,
    onPhysicalChange,
    onUsageChange
  });
  const model = (0, import_peripheralCardModel.createPeripheralConnectionCardModel)({
    row,
    runtimeOverlay: runtimeState,
    translation
  });
  const isPhone = variant === "phone";
  const submitting = isRuntimeSubmitting || removing;
  const isComponentConnection = row.connection.type === "component";
  const openPhysicalSelect = () => {
    const candidates = runtimeState == null ? void 0 : runtimeState.candidates;
    if (!(candidates == null ? void 0 : candidates.length)) return;
    addActionLog("appPeripheral-打开物理设备选择弹窗", {
      connectionId: row.id,
      candidateCount: candidates.length,
      uuids: candidates.map((device) => device.uuid)
    });
    setPhysicalSelect({ row, devices: candidates });
  };
  const handleOpenCardActions = () => {
    addActionLog("appPeripheral-点击外设卡片", {
      connectionId: row.id,
      status: model.status,
      isSelectAction: model.isSelectAction
    });
    if (model.isSelectAction) {
      openPhysicalSelect();
      return;
    }
    setActionsOpen(true);
  };
  const getHardwareInfo = () => {
    const device = model.physicalDevice || row.physical;
    if (device == null ? void 0 : device.ip) {
      return {
        label: getText("pisell2.saas.app-peripheral.hardware.ip"),
        value: device.ip
      };
    }
    if (device == null ? void 0 : device.mac) {
      return {
        label: getText("pisell2.saas.app-peripheral.hardware.mac"),
        value: device.mac
      };
    }
    if (device == null ? void 0 : device.port) {
      return {
        label: getText("pisell2.saas.app-peripheral.hardware.port"),
        value: device.port
      };
    }
    return void 0;
  };
  const hardware = getHardwareInfo();
  const handleTest = async () => {
    var _a, _b;
    addActionLog("appPeripheral-点击测试外设", {
      connectionId: row.id,
      uuid: ((_a = model.physicalDevice) == null ? void 0 : _a.uuid) || ((_b = row.physical) == null ? void 0 : _b.uuid),
      usage: row.usage
    });
    setTestLoading(true);
    try {
      await (onTest == null ? void 0 : onTest({
        connection: row.connection,
        targetDevice: row.peripheral,
        physicalDevice: model.physicalDevice || row.physical,
        peripheralUsage: row.usage
      }));
      addActionLog("appPeripheral-测试外设完成", {
        connectionId: row.id
      });
    } finally {
      setTestLoading(false);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_PeripheralConnectionCard.default,
    {
      model,
      getText,
      onOpenActions: handleOpenCardActions,
      onOpenPhysicalSelect: openPhysicalSelect,
      onConnect: () => handleConnectPeripheral(row)
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_PeripheralActionModal.default,
    {
      open: actionsOpen,
      model,
      variant,
      submitting,
      testLoading,
      getText,
      onClose: () => {
        addActionLog("appPeripheral-关闭外设操作弹窗", {
          connectionId: row.id
        });
        setActionsOpen(false);
      },
      onEdit: () => {
        addActionLog("appPeripheral-操作弹窗点击编辑", {
          connectionId: row.id,
          targetDeviceId: row.peripheralId
        });
        setActionsOpen(false);
        onEdit(row.connection);
      },
      onConnect: () => {
        addActionLog("appPeripheral-操作弹窗点击连接", {
          connectionId: row.id
        });
        handleConnectPeripheral(row);
      },
      onTest: handleTest,
      onIgnore: () => {
        addActionLog(
          isComponentConnection ? "appPeripheral-操作弹窗点击删除组件" : "appPeripheral-操作弹窗点击忘记连接",
          {
            connectionId: row.id
          }
        );
        setIgnoreConfirmOpen(true);
      },
      onSelectUsage: (usage) => {
        addActionLog("appPeripheral-选择打印机角色", {
          connectionId: row.id,
          usage
        });
        handleUpdatePeripheralUsage(row, usage);
      }
    }
  ), ignoreConfirmOpen ? /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open: true,
      title: getText(
        isComponentConnection ? "pisell2.saas.app-peripheral.delete-component" : "pisell2.saas.app-peripheral.ignore-connection"
      ),
      headerDivider: false,
      okText: getText(
        isComponentConnection ? "pisell2.saas.app-peripheral.delete-confirm" : "pisell2.saas.app-peripheral.ignore-confirm"
      ),
      cancelText: getText("pisell2.saas.app-peripheral.cancel"),
      okButtonProps: {
        block: true,
        size: "large",
        danger: true,
        loading: removing,
        onClick: async () => {
          var _a, _b;
          addActionLog(
            isComponentConnection ? "appPeripheral-确认删除组件" : "appPeripheral-确认忘记连接",
            {
              connectionId: row.id,
              uuid: ((_a = model.physicalDevice) == null ? void 0 : _a.uuid) || ((_b = row.physical) == null ? void 0 : _b.uuid)
            }
          );
          setRemoving(true);
          try {
            if (isComponentConnection) {
              await onDeleteDevice(row.connection);
            } else {
              await onRemove(row.connection);
              await handleRemovePeripheral(row);
            }
            setIgnoreConfirmOpen(false);
            setActionsOpen(false);
            addActionLog(
              isComponentConnection ? "appPeripheral-删除组件完成" : "appPeripheral-忘记连接完成",
              {
                connectionId: row.id
              }
            );
          } finally {
            setRemoving(false);
          }
        }
      },
      cancelButtonProps: {
        block: true,
        size: "large",
        type: "default",
        disabled: removing,
        onClick: () => {
          addActionLog(
            isComponentConnection ? "appPeripheral-取消删除组件确认" : "appPeripheral-取消忘记连接确认",
            {
              connectionId: row.id
            }
          );
          setIgnoreConfirmOpen(false);
        }
      },
      width: isPhone ? "100%" : 460,
      platform: isPhone ? "h5" : void 0,
      mobileModalHeight: isPhone ? "auto" : void 0,
      destroyOnClose: true,
      maskClosable: !removing,
      className: (0, import_classnames.default)("saas-app-peripheral-ignore-modal", {
        "saas-app-peripheral-ignore-modal--phone": isPhone
      }),
      onCancel: () => {
        if (removing) return;
        addActionLog(
          isComponentConnection ? "appPeripheral-关闭删除组件确认弹窗" : "appPeripheral-关闭忘记连接确认弹窗",
          {
            connectionId: row.id
          }
        );
        setIgnoreConfirmOpen(false);
      },
      bodyStyle: { paddingBottom: "24px" }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-app-peripheral-ignore-modal__desc" }, getText(
      isComponentConnection ? "pisell2.saas.app-peripheral.delete-component-desc" : "pisell2.saas.app-peripheral.ignore-desc"
    )),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-app-peripheral-ignore-modal__details" }, /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", null, getText("pisell2.saas.app-peripheral.connection-type")), /* @__PURE__ */ import_react.default.createElement("strong", null, model.connectionTypeLabel || "-")), hardware ? /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", null, hardware.label), /* @__PURE__ */ import_react.default.createElement("strong", null, hardware.value)) : null)
  ) : null, /* @__PURE__ */ import_react.default.createElement(
    import_PhysicalDeviceSelectModal.default,
    {
      state: physicalSelect,
      variant,
      submitting,
      getText,
      onClose: () => setPhysicalSelect(void 0),
      onSelect: (device) => {
        if (!physicalSelect) return;
        addActionLog("appPeripheral-选择物理设备候选", {
          connectionId: physicalSelect.row.id,
          uuid: device.uuid,
          connectType: device.connect_type
        });
        setPhysicalSelect(void 0);
        connectSelectedPhysicalDevice(physicalSelect.row, device);
      }
    }
  ));
};
var PeripheralConnectionCardContainer_default = PeripheralConnectionCardContainer;
