function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React from 'react';
import { Table, Button, Input, BaseTranslation } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { DeleteOutlined, PlusOutlined } from '@ant-design/icons';
import { useControllableValue } from 'ahooks';
import "../../index.less";
var OptionsTable = function OptionsTable(props) {
  var _useControllableValue = useControllableValue(props),
    _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
    value = _useControllableValue2[0],
    onChange = _useControllableValue2[1];

  /**
   * 更新单个选项的字段
   * @param index 选项索引
   * @param field 字段名 (label 或 value)
   * @param fieldValue 新值
   */
  var updateOptionField = function updateOptionField(index, field, fieldValue) {
    onChange === null || onChange === void 0 || onChange(function (prev) {
      var newOptions = _toConsumableArray(prev);
      newOptions[index] = _objectSpread(_objectSpread({}, (newOptions === null || newOptions === void 0 ? void 0 : newOptions[index]) || {}), {}, _defineProperty({}, field, fieldValue));
      return newOptions;
    });
  };

  /**
   * 添加新选项
   */
  var handleAdd = function handleAdd() {
    onChange === null || onChange === void 0 || onChange(function (prev) {
      return [].concat(_toConsumableArray(prev || []), [{
        id: new Date().getTime().toString(),
        label: '',
        value: '',
        color: 'default'
      }]);
    });
  };

  /**
   * 删除选项
   * @param index 要删除的选项索引
   */
  var handleRemove = function handleRemove(index) {
    onChange === null || onChange === void 0 || onChange(function (prev) {
      return (prev || []).filter(function (_, i) {
        return i !== index;
      });
    });
  };

  /**
   * 处理拖拽排序
   */
  var handleSortChange = function handleSortChange(detail) {
    var newDataSource = detail.dataSource;
    // 从新数据源中提取实际的选项数据
    var newOptions = newDataSource.map(function (item) {
      return {
        id: item.id,
        label: item.label,
        value: item.value,
        color: item.color
      };
    });
    onChange === null || onChange === void 0 || onChange(newOptions);
    return false; // 返回 false 表示排序成功
  };

  // 定义表格列
  var columns = [{
    title: locales.getText('registry.optionDataSource.options.label-field'),
    dataIndex: 'label',
    key: 'label',
    render: function render(_, record, index) {
      return /*#__PURE__*/React.createElement(BaseTranslation, {
        renderMode: "edit",
        type: "text",
        value: record.label,
        onChange: function onChange(value) {
          return updateOptionField(index, 'label', value);
        },
        placeholder: locales.getText('registry.optionDataSource.options.label-placeholder'),
        inputStyle: record.label ? {
          borderColor: '#D0D5DD'
        } : {}
      });
    }
  }, {
    title: locales.getText('registry.optionDataSource.options.value-field'),
    dataIndex: 'value',
    key: 'value',
    render: function render(_, record, index) {
      return /*#__PURE__*/React.createElement(Input, {
        value: record.value,
        onChange: function onChange(e) {
          return updateOptionField(index, 'value', e.target.value);
        },
        placeholder: locales.getText('registry.optionDataSource.options.value-placeholder'),
        style: record.value ? {
          borderColor: '#D0D5DD'
        } : {}
      });
    }
  }, {
    title: '',
    key: 'action',
    width: 80,
    align: 'center',
    render: function render(_, record, index) {
      return /*#__PURE__*/React.createElement(Button, {
        type: "text",
        danger: true,
        size: "small",
        icon: /*#__PURE__*/React.createElement(DeleteOutlined, null),
        onClick: function onClick() {
          return handleRemove(index);
        }
      });
    }
  }];
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
    type: "text",
    onClick: handleAdd,
    icon: /*#__PURE__*/React.createElement(PlusOutlined, null),
    style: {
      position: 'absolute',
      right: 0,
      top: '-36px',
      zIndex: 9
    }
  }, locales.getText('registry.optionDataSource.options.add')), /*#__PURE__*/React.createElement(Table, {
    rowKey: "id",
    columns: columns,
    dataSource: value || [],
    pagination: false,
    dragSort: true,
    virtual: false,
    tableLayout: "",
    onSortChange: handleSortChange,
    locale: {
      emptyText: locales.getText('registry.optionDataSource.options.empty')
    }
  }));
};
export default OptionsTable;