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/components/pisellFloorMapLayout/components/FloorMapDataForm.tsx
var FloorMapDataForm_exports = {};
__export(FloorMapDataForm_exports, {
  FloorMapDataForm: () => FloorMapDataForm,
  default: () => FloorMapDataForm_default
});
module.exports = __toCommonJS(FloorMapDataForm_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_FloorMapDataForm = require("./FloorMapDataForm.less");
var PREFIX = "pisell-floor-map-layout";
var I18N_DF = "pisell-floor-map-layout.data-form";
function renderFormControl(col, value) {
  var _a;
  const type = col.type ?? "string";
  if (type === "number") {
    return /* @__PURE__ */ import_react.default.createElement(import_antd.InputNumber, { style: { width: "100%" }, min: 0 });
  }
  if (type === "select" && ((_a = col.options) == null ? void 0 : _a.length)) {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Select,
      {
        allowClear: false,
        options: col.options.map((o) => ({ label: o.label, value: o.value })),
        style: { width: "100%" }
      }
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Input, null);
}
function FloorMapDataForm(props) {
  const {
    dataSourceKey,
    recordId,
    record,
    dataSources,
    onSave,
    saving,
    columns,
    className
  } = props;
  const [form] = import_antd.Form.useForm();
  const [changed, setChanged] = (0, import_react.useState)(false);
  const initialValues = import_react.default.useMemo(() => ({ ...record }), [record]);
  const handleFinish = (0, import_react.useCallback)(
    (values) => {
      onSave(dataSourceKey, recordId, values);
      setChanged(false);
    },
    [dataSourceKey, recordId, onSave]
  );
  const handleValuesChange = (0, import_react.useCallback)(() => setChanged(true), []);
  (0, import_react.useEffect)(() => {
    form.setFieldsValue(record);
  }, [recordId, record, form]);
  const fieldsToRender = columns ? columns.filter((col) => col.key !== "id") : Object.entries(record).filter(([k]) => k !== "id").map(([key]) => ({ key, label: key, type: "string" }));
  if (fieldsToRender.length === 0) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(`${PREFIX}-data-form`, className) }, /* @__PURE__ */ import_react.default.createElement("p", { className: `${PREFIX}-data-form-empty` }, import_utils.locales.getText(`${I18N_DF}.empty`)));
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(`${PREFIX}-data-form`, className) }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form,
    {
      form,
      key: recordId,
      layout: "vertical",
      size: "small",
      initialValues,
      onFinish: handleFinish,
      onValuesChange: handleValuesChange
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { name: "id", label: "ID", hidden: true }, /* @__PURE__ */ import_react.default.createElement(import_antd.Input, { disabled: true })),
    fieldsToRender.map((col) => /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { key: col.key, name: col.key, label: col.label }, renderFormControl(col, record[col.key]))),
    /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, null, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        type: "primary",
        htmlType: "submit",
        loading: saving,
        disabled: !changed
      },
      import_utils.locales.getText(`${I18N_DF}.save-record`)
    ))
  ));
}
var FloorMapDataForm_default = FloorMapDataForm;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FloorMapDataForm
});
