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/systemSettings/registry/components/OptionDataSource/OptionTable.tsx
var OptionTable_exports = {};
__export(OptionTable_exports, {
  default: () => OptionTable_default
});
module.exports = __toCommonJS(OptionTable_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_icons = require("@ant-design/icons");
var import_ahooks = require("ahooks");
var import__ = require("../../index.less");
var OptionsTable = (props) => {
  const [value, onChange] = (0, import_ahooks.useControllableValue)(props);
  const updateOptionField = (index, field, fieldValue) => {
    onChange == null ? void 0 : onChange((prev) => {
      const newOptions = [...prev];
      newOptions[index] = {
        ...(newOptions == null ? void 0 : newOptions[index]) || {},
        [field]: fieldValue
      };
      return newOptions;
    });
  };
  const handleAdd = () => {
    onChange == null ? void 0 : onChange((prev) => {
      return [
        ...prev || [],
        {
          id: (/* @__PURE__ */ new Date()).getTime().toString(),
          label: "",
          value: "",
          color: "default"
        }
      ];
    });
  };
  const handleRemove = (index) => {
    onChange == null ? void 0 : onChange((prev) => {
      return (prev || []).filter((_, i) => i !== index);
    });
  };
  const handleSortChange = (detail) => {
    const { dataSource: newDataSource } = detail;
    const newOptions = newDataSource.map((item) => ({
      id: item.id,
      label: item.label,
      value: item.value,
      color: item.color
    }));
    onChange == null ? void 0 : onChange(newOptions);
    return false;
  };
  const columns = [
    {
      title: import_utils.locales.getText("registry.optionDataSource.options.label-field"),
      dataIndex: "label",
      key: "label",
      render: (_, record, index) => {
        return /* @__PURE__ */ import_react.default.createElement(
          import_materials.BaseTranslation,
          {
            renderMode: "edit",
            type: "text",
            value: record.label,
            onChange: (value2) => updateOptionField(index, "label", value2),
            placeholder: import_utils.locales.getText(
              "registry.optionDataSource.options.label-placeholder"
            ),
            inputStyle: record.label ? { borderColor: "#D0D5DD" } : {}
          }
        );
      }
    },
    {
      title: import_utils.locales.getText("registry.optionDataSource.options.value-field"),
      dataIndex: "value",
      key: "value",
      render: (_, record, index) => {
        return /* @__PURE__ */ import_react.default.createElement(
          import_materials.Input,
          {
            value: record.value,
            onChange: (e) => updateOptionField(index, "value", e.target.value),
            placeholder: import_utils.locales.getText(
              "registry.optionDataSource.options.value-placeholder"
            ),
            style: record.value ? { borderColor: "#D0D5DD" } : {}
          }
        );
      }
    },
    {
      title: "",
      key: "action",
      width: 80,
      align: "center",
      render: (_, record, index) => {
        return /* @__PURE__ */ import_react.default.createElement(
          import_materials.Button,
          {
            type: "text",
            danger: true,
            size: "small",
            icon: /* @__PURE__ */ import_react.default.createElement(import_icons.DeleteOutlined, null),
            onClick: () => handleRemove(index)
          }
        );
      }
    }
  ];
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_materials.Button,
    {
      type: "text",
      onClick: handleAdd,
      icon: /* @__PURE__ */ import_react.default.createElement(import_icons.PlusOutlined, null),
      style: { position: "absolute", right: 0, top: "-36px", zIndex: 9 }
    },
    import_utils.locales.getText("registry.optionDataSource.options.add")
  ), /* @__PURE__ */ import_react.default.createElement(
    import_materials.Table,
    {
      rowKey: "id",
      columns,
      dataSource: value || [],
      pagination: false,
      dragSort: true,
      virtual: false,
      tableLayout: "",
      onSortChange: handleSortChange,
      locale: {
        emptyText: import_utils.locales.getText("registry.optionDataSource.options.empty")
      }
    }
  ));
};
var OptionTable_default = OptionsTable;
