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/appDevicePlanning/modules/devices/components/EditDeviceModal/index.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_icons = require("@ant-design/icons");
var import_constants = require("../../../../model/constants");
var import_types = require("../../../../model/types");
var import_index = require("./index.less");
var isProfileRequired = (model) => {
  var _a, _b;
  return Boolean((_b = (_a = model == null ? void 0 : model.data) == null ? void 0 : _a.profile_structure) == null ? void 0 : _b.required);
};
var EditDeviceModal = ({
  open,
  variant = "desktop",
  record,
  workAreas,
  model,
  submitting,
  getText,
  translation,
  onCancel,
  onSubmit
}) => {
  const isPhoneVariant = variant === "phone";
  const [form] = import_antd.Form.useForm();
  const currentModel = model || (record == null ? void 0 : record._raw.model);
  const profileRequired = isProfileRequired(currentModel);
  const profiles = (currentModel == null ? void 0 : currentModel.active_profiles) || [];
  const online = (record == null ? void 0 : record.status) === "online";
  const showShortNumber = (record == null ? void 0 : record._raw.type_kind) === "smart";
  (0, import_react.useEffect)(() => {
    if (!open || !record) return;
    form.setFieldsValue({
      workarea_id: record._raw.workarea_id,
      name: (0, import_types.toLangDict)(record._raw.name),
      short_number: record._raw.short_number || "",
      profile_id: record._raw.profile_id || void 0
    });
  }, [form, open, record]);
  const handleOk = async () => {
    if (!record) return;
    const values = await form.validateFields();
    const payload = {
      workarea_id: values.workarea_id,
      name: values.name,
      profile_id: values.profile_id || null
    };
    if (showShortNumber) {
      payload.short_number = (0, import_constants.normalizeDeviceShortNumberPayload)(
        values.short_number
      );
    }
    onSubmit(record.id, payload);
  };
  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: { loading: submitting, onClick: handleOk },
      cancelButtonProps: { disabled: submitting },
      destroyOnClose: true,
      maskClosable: false,
      width: 620,
      platform: isPhoneVariant ? "h5" : void 0,
      mobileModalHeight: isPhoneVariant ? "auto" : void 0,
      className: `edit-device-modal edit-device-modal--${variant}`
    },
    record ? /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", preserve: false }, online ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Alert,
      {
        type: "warning",
        showIcon: true,
        icon: /* @__PURE__ */ import_react.default.createElement(import_icons.ExclamationCircleFilled, null),
        message: getText(
          "pisell2.saas.device-planning.edit.online-title"
        ),
        description: getText(
          "pisell2.saas.device-planning.edit.online-desc"
        ),
        style: { marginBottom: 24 }
      }
    ) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: "edit-device-modal__row" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        label: getText("pisell2.saas.device-planning.detail.type"),
        className: "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: "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_antd.Select,
        {
          placeholder: getText(
            "pisell2.saas.device-planning.add.work-area-placeholder"
          ),
          options: workAreas.map((item) => ({
            label: translation(item.name),
            value: item.id
          }))
        }
      )
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: "name",
        label: getText("pisell2.saas.device-planning.detail.name"),
        required: true,
        rules: [
          {
            validator: (0, import_types.validateI18nRequired)(
              getText("pisell2.saas.device-planning.add.device-name-required")
            )
          }
        ]
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_materials.BaseTranslation,
        {
          renderMode: "edit",
          placeholder: getText(
            "pisell2.saas.device-planning.add.device-name-placeholder"
          )
        }
      )
    ), showShortNumber ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: "short_number",
        label: getText(
          "pisell2.saas.device-planning.device-short-number.label"
        ),
        normalize: (value) => (0, import_constants.normalizeDeviceShortNumberInput)(value),
        required: true,
        rules: [
          {
            validator: (0, import_constants.validateDeviceShortNumber)({
              required: getText(
                "pisell2.saas.device-planning.device-short-number.required"
              ),
              format: getText(
                "pisell2.saas.device-planning.device-short-number.format"
              ),
              reserved: getText(
                "pisell2.saas.device-planning.device-short-number.reserved"
              )
            })
          }
        ],
        extra: getText(
          "pisell2.saas.device-planning.device-short-number.helper"
        )
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Input,
        {
          maxLength: import_constants.DEVICE_SHORT_NUMBER_MAX_LENGTH,
          placeholder: getText(
            "pisell2.saas.device-planning.device-short-number.placeholder"
          )
        }
      )
    ) : null, profileRequired || profiles.length > 0 ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: "profile_id",
        label: getText("pisell2.saas.device-planning.detail.profile"),
        required: profileRequired,
        rules: [
          {
            required: profileRequired,
            message: getText(
              "pisell2.saas.device-planning.add.device-profile-required"
            )
          }
        ],
        help: profileRequired && profiles.length === 0 ? getText(
          "pisell2.saas.device-planning.add.create-profile-first"
        ) : void 0
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Select,
        {
          disabled: profiles.length === 0,
          placeholder: getText(
            "pisell2.saas.device-planning.add.device-profile-placeholder"
          ),
          options: profiles.map((profile) => ({
            label: translation(profile.name) || String(profile.id),
            value: profile.id
          }))
        }
      )
    ) : null) : null
  );
};
var EditDeviceModal_default = EditDeviceModal;
