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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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); }
import React from 'react';
import { isFunction } from '@pisell/utils';
import { formFieldMap } from "../../fields";
export var genGetField = function genGetField(dataSource) {
  var _ref = dataSource || {},
    _ref$fields = _ref.fields,
    fields = _ref$fields === void 0 ? [] : _ref$fields;
  var fieldMap = fields.reduce(function (acc, field) {
    acc[field.name] = field;
    return acc;
  }, {});
  return function (key) {
    return fieldMap[key];
  };
};
var fieldType2FilterMap = {
  Checkbox: 'FormItemSelect',
  ColorPicker: 'FormItemInput',
  DatePicker: 'FormItemDateRangePicker',
  Input: 'FormItemInput',
  Radio: 'FormItemSelect',
  Select: 'FormItemSelect',
  Percent: 'FormItemPercent',
  InputNumber: 'FormItemInputNumber',
  'Input.JSON': 'FormItemInput',
  'Input.Email': 'FormItemInput',
  'Input.URL': 'FormItemInput',
  'Input.Password': 'FormItemInput',
  'Radio.Group': 'FormItemSelect',
  'Checkbox.Group': 'FormItemSelect',
  'Translation': 'FormItemTranslation'
};
var getCptProps = function getCptProps(field, cptType, item) {
  var _field$uiSchema, _props, _field$uiSchema2, _field$uiSchema3, _field$uiSchema6, _field$uiSchema8, _item$other;
  var props = (field === null || field === void 0 || (_field$uiSchema = field.uiSchema) === null || _field$uiSchema === void 0 ? void 0 : _field$uiSchema['x-component-props']) || {};
  props.style = _objectSpread(_objectSpread({}, ((_props = props) === null || _props === void 0 ? void 0 : _props.style) || {}), {}, {
    width: '200px',
    margin: 0
  });
  props.placeholder = item.label;
  props.allowClear = true;
  if (cptType === 'FormItemSelect') {
    props.mode = 'multiple';
    props.showSearch = true;
    props.optionFilterProp = 'label';
  }
  if (['Select', 'Radio.Group', 'Checkbox.Group'].includes(field === null || field === void 0 || (_field$uiSchema2 = field.uiSchema) === null || _field$uiSchema2 === void 0 ? void 0 : _field$uiSchema2['x-component']) && ((field === null || field === void 0 || (_field$uiSchema3 = field.uiSchema) === null || _field$uiSchema3 === void 0 || (_field$uiSchema3 = _field$uiSchema3.enum) === null || _field$uiSchema3 === void 0 ? void 0 : _field$uiSchema3.length) || 0) > 0) {
    var _field$uiSchema4, _field$uiSchema5;
    if (['Radio.Group', 'Checkbox.Group'].includes(field === null || field === void 0 || (_field$uiSchema4 = field.uiSchema) === null || _field$uiSchema4 === void 0 ? void 0 : _field$uiSchema4['x-component'])) {
      props.direction = 'horizontal';
    } else {
      props.showSearch = true;
      props.optionFilterProp = 'label';
    }
    props.options = field === null || field === void 0 || (_field$uiSchema5 = field.uiSchema) === null || _field$uiSchema5 === void 0 ? void 0 : _field$uiSchema5.enum;
  }
  if ((field === null || field === void 0 || (_field$uiSchema6 = field.uiSchema) === null || _field$uiSchema6 === void 0 ? void 0 : _field$uiSchema6['x-component']) === 'ColorPicker') {
    var _field$uiSchema7;
    props.defaultValue = field === null || field === void 0 || (_field$uiSchema7 = field.uiSchema) === null || _field$uiSchema7 === void 0 ? void 0 : _field$uiSchema7.default;
  }
  if (cptType === 'DateRangePicker') {
    var _props2;
    props.style = _objectSpread(_objectSpread({}, ((_props2 = props) === null || _props2 === void 0 ? void 0 : _props2.style) || {}), {}, {
      width: '240px'
    });
  }

  // 勾选组件
  if ((field === null || field === void 0 || (_field$uiSchema8 = field.uiSchema) === null || _field$uiSchema8 === void 0 ? void 0 : _field$uiSchema8['x-component']) === 'Checkbox' && cptType === 'Select') {
    props.mode = undefined;
    props.allowClear = true;
    props.options = [{
      label: '是',
      value: true
    }, {
      label: '否',
      value: false
    }];
  }
  props = _objectSpread(_objectSpread({}, props), (item === null || item === void 0 || (_item$other = item.other) === null || _item$other === void 0 ? void 0 : _item$other.fieldProps) || {});
  return props;
};
export var field2Cpt = function field2Cpt(field, item, otherProps) {
  var isCustom = item.isCustom,
    configType = item.type;
  var _ref2 = field || {},
    uiSchema = _ref2.uiSchema;
  var _ref3 = uiSchema || {},
    xComponent = _ref3['x-component'];
  var cptType = fieldType2FilterMap[xComponent];
  if (isCustom) {
    cptType = configType;
  }
  var Cpt = formFieldMap[cptType];
  if (!Cpt) {
    return null;
  }
  var props = getCptProps(field, cptType, item);
  var style = isFunction(otherProps === null || otherProps === void 0 ? void 0 : otherProps.style) ? otherProps === null || otherProps === void 0 ? void 0 : otherProps.style(props.style) : otherProps !== null && otherProps !== void 0 && otherProps.style ? _objectSpread(_objectSpread({}, props.style), otherProps === null || otherProps === void 0 ? void 0 : otherProps.style) : props.style;
  return /*#__PURE__*/React.createElement(Cpt, _extends({}, props, {
    label: item.label,
    name: item.name || item.value
  }, otherProps, {
    style: style
  }));
};
export var genFilterItems = function genFilterItems(list, dataSource) {
  return list.map(function (item) {
    if (!item) {
      return null;
    }
    var name = item.name;
    var getField = genGetField(dataSource);
    var field = getField(name);
    var cptDom = field2Cpt(field, item);
    return cptDom;
  });
};
var FILTER_STORAGE_KEY = 'filter-setting-';
var getStorageKey = function getStorageKey(componentId) {
  return "".concat(FILTER_STORAGE_KEY).concat(componentId);
};
export var getStoredFilterOrder = function getStoredFilterOrder(componentId) {
  try {
    var stored = localStorage.getItem(getStorageKey(componentId));
    return stored ? JSON.parse(stored) : null;
  } catch (e) {
    console.error('Failed to parse stored filter order:', e);
    return null;
  }
};
export var saveFilterOrder = function saveFilterOrder(componentId, order) {
  try {
    localStorage.setItem(getStorageKey(componentId), JSON.stringify(order));
  } catch (e) {
    console.error('Failed to save filter order:', e);
  }
};