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/EditDeviceModal.tsx
var EditDeviceModal_exports = {};
__export(EditDeviceModal_exports, {
  default: () => EditDeviceModal_default
});
module.exports = __toCommonJS(EditDeviceModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_classnames = __toESM(require("classnames"));
var import_types = require("../types");
var import_utils = require("../utils");
var import_I18nNameFormItem = __toESM(require("./shared/I18nNameFormItem"));
var EditDeviceModal = ({
  open,
  record,
  variant,
  workAreas,
  models,
  submitting,
  getText,
  translation,
  onCancel,
  onSubmit
}) => {
  const [form] = import_antd.Form.useForm();
  const isPhoneVariant = variant === "phone";
  const currentModel = (0, import_react.useMemo)(() => {
    if (!record) return void 0;
    return models.find((item) => item.code === record._raw.model_code) || record._raw.model;
  }, [models, record]);
  const profileRequired = (0, import_utils.isProfileRequired)(currentModel);
  const profiles = (currentModel == null ? void 0 : currentModel.active_profiles) || [];
  (0, import_react.useEffect)(() => {
    if (!open || !record) return;
    form.setFieldsValue({
      workarea_id: record._raw.workarea_id,
      name: (0, import_types.toLangDict)(record._raw.name),
      description: (0, import_types.toLangDict)(record._raw.description),
      profile_id: record._raw.profile_id || void 0
    });
  }, [form, open, record]);
  const handleOk = async () => {
    if (!record) return;
    const values = await form.validateFields();
    onSubmit(record.id, {
      workarea_id: values.workarea_id,
      name: values.name,
      description: values.description,
      profile_id: null
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      title: getText("pisell2.saas.device-planning.edit.title"),
      headerDivider: false,
      onCancel,
      okText: getText("pisell2.saas.device-planning.detail.save"),
      cancelText: getText("pisell2.saas.device-planning.detail.cancel"),
      okButtonProps: {
        block: true,
        size: "large",
        loading: submitting,
        onClick: handleOk
      },
      cancelButtonProps: {
        block: true,
        size: "large",
        type: "default",
        disabled: submitting
      },
      destroyOnClose: true,
      maskClosable: false,
      width: isPhoneVariant ? "100%" : 520,
      platform: isPhoneVariant ? "h5" : void 0,
      mobileModalHeight: isPhoneVariant ? "auto" : void 0,
      className: (0, import_classnames.default)("saas-app-peripheral-edit-device-modal", {
        "saas-app-peripheral-edit-device-modal--phone": isPhoneVariant
      })
    },
    record ? /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", preserve: false }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-app-peripheral-edit-device-modal__row" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        label: getText("pisell2.saas.device-planning.detail.type"),
        className: "saas-app-peripheral-edit-device-modal__col"
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Input, { value: record.typeName, disabled: true })
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        label: getText("pisell2.saas.device-planning.detail.model"),
        className: "saas-app-peripheral-edit-device-modal__col"
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Input, { value: record.modelName, disabled: true })
    )), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: "workarea_id",
        label: getText("pisell2.saas.device-planning.edit.work-area"),
        required: true,
        rules: [
          {
            required: true,
            message: getText(
              "pisell2.saas.device-planning.edit.work-area-required"
            )
          }
        ]
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_materials.Select,
        {
          options: workAreas.map((item) => ({
            label: translation(item.name),
            value: item.id
          }))
        }
      )
    ), /* @__PURE__ */ import_react.default.createElement(
      import_I18nNameFormItem.default,
      {
        name: "name",
        label: getText("pisell2.saas.device-planning.detail.name"),
        requiredMessage: getText(
          "pisell2.saas.device-planning.add.device-name-required"
        )
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      import_I18nNameFormItem.default,
      {
        name: "description",
        label: getText("pisell2.saas.device-planning.modal.description"),
        placeholder: getText(
          "pisell2.saas.device-planning.modal.description-placeholder"
        ),
        textarea: true
      }
    ), profileRequired && profiles.length > 0 ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: "profile_id",
        label: getText("pisell2.saas.device-planning.detail.profile")
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_materials.Select,
        {
          options: profiles.map((profile) => ({
            label: translation(profile.name) || String(profile.id),
            value: profile.id
          }))
        }
      )
    ) : null) : null
  );
};
var EditDeviceModal_default = EditDeviceModal;
