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_constants = require("../constants");
var import_usePeripheralRuntime = require("../hooks/usePeripheralRuntime");
var import_peripheralCardModel = require("../utils/peripheralCardModel");
var import_PeripheralConnectionCard = __toESM(require("./PeripheralConnectionCard"));
var import_PhysicalDeviceSelectModal = __toESM(require("./PhysicalDeviceSelectModal"));
var import_PrinterUsageModal = __toESM(require("./PrinterUsageModal"));
var getPeripheralName = (connection, translation) => {
  const peripheral = connection == null ? void 0 : connection.target_device;
  return peripheral ? translation(peripheral.name) || String(peripheral.id) : "-";
};
var PeripheralConnectionCardContainer = ({
  row,
  variant,
  getText,
  translation,
  getAppInstance,
  onPhysicalChange,
  onUsageChange,
  onTest,
  onEdit,
  onRemove
}) => {
  const [disconnecting, setDisconnecting] = (0, import_react.useState)(false);
  const {
    runtimeState,
    physicalSelect,
    printUsageSelect,
    isRuntimeSubmitting,
    setPhysicalSelect,
    setPrintUsageSelect,
    handleConnectPeripheral,
    handleDisconnectPeripheral,
    handleOpenUsageSelect,
    handleSelectPeripheralUsage,
    connectSelectedPhysicalDevice
  } = (0, import_usePeripheralRuntime.usePeripheralRuntime)({
    row,
    getAppInstance,
    getText,
    onPhysicalChange,
    onUsageChange
  });
  const model = (0, import_peripheralCardModel.createPeripheralConnectionCardModel)({
    row,
    runtimeOverlay: runtimeState,
    getText,
    translation
  });
  const isPhone = variant === "phone";
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_PeripheralConnectionCard.default,
    {
      model,
      getText,
      onEdit,
      onRemove,
      onRequestDisconnect: () => setDisconnecting(true),
      onOpenUsageSelect: () => handleOpenUsageSelect(row),
      onTest: () => {
        onTest == null ? void 0 : onTest({
          connection: row.connection,
          targetDevice: row.peripheral,
          physicalDevice: model.physicalDevice || row.physical,
          peripheralUsage: row.usage
        });
      },
      onOpenPhysicalSelect: () => {
        const candidates = runtimeState == null ? void 0 : runtimeState.candidates;
        if (!(candidates == null ? void 0 : candidates.length)) return;
        setPhysicalSelect({ row, devices: candidates });
      },
      onConnect: () => handleConnectPeripheral(row)
    }
  ), disconnecting ? /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open: true,
      title: getText("pisell2.saas.app-peripheral.disconnect.title"),
      headerDivider: false,
      okText: getText("pisell2.saas.app-peripheral.confirm"),
      cancelText: getText("pisell2.saas.app-peripheral.cancel"),
      okButtonProps: {
        block: true,
        size: "large",
        danger: true,
        loading: isRuntimeSubmitting,
        onClick: async () => {
          await handleDisconnectPeripheral(row);
          setDisconnecting(false);
        }
      },
      cancelButtonProps: {
        block: true,
        size: "large",
        type: "default",
        disabled: isRuntimeSubmitting,
        onClick: () => setDisconnecting(false)
      },
      width: 460,
      platform: isPhone ? "h5" : void 0,
      mobileModalHeight: isPhone ? "auto" : void 0,
      className: (0, import_classnames.default)("saas-app-peripheral-confirm-modal", {
        "saas-app-peripheral-confirm-modal--phone": isPhone
      }),
      destroyOnClose: true,
      maskClosable: !isRuntimeSubmitting,
      onCancel: () => {
        if (isRuntimeSubmitting) return;
        setDisconnecting(false);
      }
    },
    (0, import_constants.formatToken)(
      getText("pisell2.saas.app-peripheral.disconnect.confirm"),
      { name: getPeripheralName(row.connection, translation) }
    )
  ) : null, /* @__PURE__ */ import_react.default.createElement(
    import_PhysicalDeviceSelectModal.default,
    {
      state: physicalSelect,
      variant,
      submitting: isRuntimeSubmitting,
      getText,
      onClose: () => setPhysicalSelect(void 0),
      onSelect: (device) => {
        if (!physicalSelect) return;
        setPhysicalSelect(void 0);
        connectSelectedPhysicalDevice(physicalSelect.row, device);
      }
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_PrinterUsageModal.default,
    {
      state: printUsageSelect,
      variant,
      submitting: isRuntimeSubmitting,
      getText,
      onClose: () => setPrintUsageSelect(void 0),
      onSelect: (usage) => {
        handleSelectPeripheralUsage(usage);
      }
    }
  ));
};
var PeripheralConnectionCardContainer_default = PeripheralConnectionCardContainer;
