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/ConnectExistingDeviceModal.tsx
var ConnectExistingDeviceModal_exports = {};
__export(ConnectExistingDeviceModal_exports, {
  default: () => ConnectExistingDeviceModal_default
});
module.exports = __toCommonJS(ConnectExistingDeviceModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_icons = require("@ant-design/icons");
var import_classnames = __toESM(require("classnames"));
var import_types = require("../types");
var import_useConnectDeviceModal = require("../hooks/useConnectDeviceModal");
var import_ConnectDeviceModalHeader = __toESM(require("./connectDeviceModal/ConnectDeviceModalHeader"));
var import_ConnectionConfigFields = __toESM(require("./connectDeviceModal/ConnectionConfigFields"));
var import_DeviceConnectionSummary = __toESM(require("./connectDeviceModal/DeviceConnectionSummary"));
var import_NewPeripheralInfoFields = __toESM(require("./connectDeviceModal/NewPeripheralInfoFields"));
var import_PickExistingDeviceStep = __toESM(require("./connectDeviceModal/PickExistingDeviceStep"));
var import_PickNewPeripheralStep = __toESM(require("./connectDeviceModal/PickNewPeripheralStep"));
var ConnectExistingDeviceModal = ({
  open,
  mode,
  connectionType,
  targetStructure,
  currentDevice,
  connectedDeviceIds,
  currentWorkAreaId,
  variant,
  getText,
  translation,
  submitting,
  onClose,
  onSubmit
}) => {
  const [form] = import_antd.Form.useForm();
  const isPhoneVariant = variant === "phone";
  const modal = (0, import_useConnectDeviceModal.useConnectDeviceModal)({
    open,
    mode,
    connectionType,
    targetStructure,
    currentDevice,
    connectedDeviceIds,
    currentWorkAreaId,
    getText,
    translation,
    onSubmit
  });
  const {
    step,
    workAreas,
    brands,
    models,
    selectedWorkAreaId,
    selectedModel,
    keyword,
    loading,
    pickingDeviceId,
    currentIsSmart,
    targetKind,
    peripheral,
    smartDevice,
    profileOptions,
    profileRequired,
    connectionTypes,
    filteredDevices,
    getWorkAreaDeviceCount,
    getDeviceCountText,
    setSelectedWorkAreaId,
    setKeyword,
    handlePickDevice,
    handlePickModel,
    handleBackToPick,
    handleSubmit
  } = modal;
  const selectedConnectionType = import_antd.Form.useWatch("connection_type", form);
  const networkMethod = import_antd.Form.useWatch("connection_method", form) || "auto";
  const newDeviceWorkAreaId = import_antd.Form.useWatch("workarea_id", form);
  const selectedTargetId = currentIsSmart ? peripheral == null ? void 0 : peripheral.id : smartDevice == null ? void 0 : smartDevice.id;
  (0, import_react.useEffect)(() => {
    if (!open) return;
    form.resetFields();
    form.setFieldsValue({
      name: { ...import_types.EMPTY_I18N },
      description: { ...import_types.EMPTY_I18N },
      workarea_id: currentWorkAreaId,
      connection_method: "auto",
      profile_ids: []
    });
  }, [currentWorkAreaId, form, open]);
  (0, import_react.useEffect)(() => {
    var _a;
    if (!open || mode !== "new" || step !== "config" || !selectedModel) return;
    const nextName = translation(selectedModel.name) || selectedModel.code;
    form.setFieldsValue({
      name: { ...import_types.EMPTY_I18N, original: `${nextName} 1` },
      description: { ...import_types.EMPTY_I18N },
      workarea_id: form.getFieldValue("workarea_id") || currentWorkAreaId || ((_a = workAreas[0]) == null ? void 0 : _a.id),
      connection_type: void 0,
      connection_method: "auto",
      connection_ip: void 0,
      profile_ids: []
    });
  }, [
    currentWorkAreaId,
    form,
    mode,
    open,
    selectedModel == null ? void 0 : selectedModel.code,
    step,
    translation,
    workAreas
  ]);
  (0, import_react.useEffect)(() => {
    if (!open || mode !== "existing" || step !== "config" || !selectedTargetId) {
      return;
    }
    form.setFieldsValue({
      connection_type: void 0,
      connection_method: "auto",
      connection_ip: void 0,
      profile_ids: []
    });
  }, [form, mode, open, selectedTargetId, step]);
  const handleValidateAndSubmit = async () => {
    const values = await form.validateFields();
    handleSubmit(values);
  };
  const renderPickStep = () => /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: `saas-app-peripheral-connect__picker is-${mode}`
    },
    mode === "new" ? /* @__PURE__ */ import_react.default.createElement(
      import_PickNewPeripheralStep.default,
      {
        loading,
        models,
        brands,
        getText,
        translation,
        onSelect: handlePickModel
      }
    ) : /* @__PURE__ */ import_react.default.createElement(
      import_PickExistingDeviceStep.default,
      {
        workAreas,
        selectedWorkAreaId,
        currentWorkAreaId,
        filteredDevices,
        connectedDeviceIds,
        loading,
        pickingDeviceId,
        keyword,
        targetKind,
        getText,
        translation,
        getWorkAreaDeviceCount,
        getDeviceCountText,
        isPhone: isPhoneVariant,
        onWorkAreaChange: setSelectedWorkAreaId,
        onKeywordChange: setKeyword,
        onPickDevice: handlePickDevice
      }
    )
  );
  const renderConfigStep = () => /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-app-peripheral-connect__config" }, /* @__PURE__ */ import_react.default.createElement(
    import_DeviceConnectionSummary.default,
    {
      mode,
      smartDevice,
      peripheral,
      selectedModel,
      translation
    }
  ), /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", preserve: false }, mode === "new" ? /* @__PURE__ */ import_react.default.createElement(
    import_NewPeripheralInfoFields.default,
    {
      workAreas,
      workAreaId: newDeviceWorkAreaId,
      currentWorkAreaId,
      getText,
      translation
    }
  ) : null, /* @__PURE__ */ import_react.default.createElement(
    import_ConnectionConfigFields.default,
    {
      connectionType: selectedConnectionType,
      networkMethod,
      profileRequired,
      profileOptions,
      connectionTypes,
      getText
    }
  )));
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      width: isPhoneVariant ? "100%" : step === "pick" ? "80vw" : "860px",
      okVisible: step === "config",
      okText: getText("pisell2.saas.device-planning.add.save"),
      cancelText: getText("pisell2.saas.device-planning.add.cancel"),
      okButtonProps: {
        block: true,
        size: "large",
        loading: submitting,
        onClick: handleValidateAndSubmit
      },
      cancelButtonProps: {
        block: true,
        size: "large",
        type: "default",
        onClick: onClose
      },
      title: /* @__PURE__ */ import_react.default.createElement(
        import_ConnectDeviceModalHeader.default,
        {
          mode,
          step,
          connectionType,
          currentDevice,
          currentIsSmart,
          getText,
          translation,
          onBack: handleBackToPick
        }
      ),
      closeIcon: /* @__PURE__ */ import_react.default.createElement(import_icons.CloseOutlined, null),
      onCancel: onClose,
      platform: isPhoneVariant ? "h5" : void 0,
      mobileModalHeight: isPhoneVariant ? "92dvh" : void 0,
      className: (0, import_classnames.default)("saas-app-peripheral-connect__modal", {
        "saas-app-peripheral-connect__modal--phone": isPhoneVariant
      }),
      bodyStyle: {
        padding: isPhoneVariant ? "16px" : "24px",
        overflow: step === "pick" ? "hidden" : "auto"
      }
    },
    step === "pick" ? renderPickStep() : renderConfigStep()
  );
};
var ConnectExistingDeviceModal_default = ConnectExistingDeviceModal;
