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 _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 _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 _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; }
import React, { useMemo, useCallback } from 'react';
import { EyeOutlined, EyeInvisibleOutlined, HolderOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import { PisellText, SortableList } from '@pisell/materials';
import "./index.less";
var Editable = PisellText.Editable;
// import Editable from '@pisell/materials/src/components/pisellText/components/Editable';

// Types

/** 默认的钱包通行证类型配置 */

var WALLET_PASS_TYPES = ['rechargeable', 'point', 'voucher', 'discount'];

// 默认的钱包通行证类型配置
export var DEFAULT_WALLET_PASS_TYPES_CONFIG = [{
  type: 'rechargeable',
  custom_title: {
    original: 'Balance',
    en: 'Balance',
    'zh-CN': '余额',
    'zh-HK': '餘額',
    'ja': '残高',
    'pt': 'Saldo'
  }
}, {
  type: 'point',
  custom_title: {
    original: 'Points',
    en: 'Points',
    'zh-CN': '积分',
    'zh-HK': '積分',
    'ja': 'ポイント',
    'pt': 'Pontos'
  }
}, {
  type: 'voucher',
  custom_title: {
    original: 'Voucher',
    en: 'Voucher',
    'zh-CN': '代金券',
    'zh-HK': '代金券',
    'ja': 'クーポン',
    'pt': 'Cupom'
  }
}, {
  type: 'discount',
  custom_title: {
    original: 'Discount',
    en: 'Discount',
    'zh-CN': '折扣券',
    'zh-HK': '折扣券',
    'ja': '割引カード',
    'pt': 'Cartão de desconto'
  }
}];
var SortableWalletPassTypes = function SortableWalletPassTypes(props) {
  var _props$value = props.value,
    value = _props$value === void 0 ? [] : _props$value,
    onChange = props.onChange,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    className = props.className,
    style = props.style;

  // 生成完整的数据源，确保四种类型都存在，保持外部传入的顺序
  var dataSource = useMemo(function () {
    var result = [];
    var processedTypes = new Set();

    // 首先按照 value 的顺序添加已存在的项
    value.forEach(function (item) {
      if (WALLET_PASS_TYPES.includes(item.type) && !processedTypes.has(item.type)) {
        result.push(item);
        processedTypes.add(item.type);
      }
    });

    // 然后添加缺失的默认类型
    DEFAULT_WALLET_PASS_TYPES_CONFIG.forEach(function (defaultType) {
      if (!processedTypes.has(defaultType.type)) {
        result.push({
          type: defaultType.type,
          display: 0,
          custom_title: defaultType.custom_title
        });
      }
    });
    return result;
  }, [value]);

  // 处理排序变化
  var handleSortChange = useCallback(function (newList) {
    if (!onChange || disabled) return;
    onChange(newList);
  }, [onChange, disabled]);

  // 处理显示/隐藏切换
  var handleToggleDisplay = useCallback(function (index) {
    if (!onChange || disabled) return;
    var newList = _toConsumableArray(dataSource);
    newList[index] = _objectSpread(_objectSpread({}, newList[index]), {}, {
      display: newList[index].display === 1 ? 0 : 1
    });
    onChange(newList);
  }, [dataSource, onChange, disabled]);

  // 处理标题编辑
  var handleTitleChange = useCallback(function (index, newTitle) {
    if (!onChange || disabled) return;
    var newList = _toConsumableArray(dataSource);
    newList[index] = _objectSpread(_objectSpread({}, newList[index]), {}, {
      custom_title: typeof newTitle === 'string' ? {
        original: newTitle,
        en: newTitle
      } : newTitle
    });
    onChange(newList);
  }, [dataSource, onChange, disabled]);

  // 获取显示标题
  var getDisplayTitle = useCallback(function (item) {
    var defaultType = DEFAULT_WALLET_PASS_TYPES_CONFIG.find(function (t) {
      return t.type === item.type;
    });
    return item.custom_title || (defaultType === null || defaultType === void 0 ? void 0 : defaultType.custom_title) || {
      original: item.type,
      en: item.type
    };
  }, []);

  // 渲染单个项
  var renderItem = useCallback(function (item, index) {
    var displayTitle = getDisplayTitle(item);
    var isVisible = item.display === 1;
    return /*#__PURE__*/React.createElement("div", {
      className: classNames('sortable-wallet-pass-item', {
        'sortable-wallet-pass-item-disabled': disabled,
        'sortable-wallet-pass-item-hidden': !isVisible
      })
    }, /*#__PURE__*/React.createElement("div", {
      className: "sortable-wallet-pass-drag-handle"
    }, /*#__PURE__*/React.createElement(HolderOutlined, {
      className: "drag-dots",
      size: 20
    })), /*#__PURE__*/React.createElement("div", {
      className: "sortable-wallet-pass-title"
    }, /*#__PURE__*/React.createElement(Editable, {
      value: displayTitle,
      editable: !disabled && {
        maxLength: 10
      },
      onChange: function onChange(newValue) {
        return handleTitleChange(index, newValue);
      }
    })), /*#__PURE__*/React.createElement("div", {
      className: "sortable-wallet-pass-toggle"
    }, /*#__PURE__*/React.createElement("button", {
      type: "button",
      style: {
        cursor: 'pointer',
        border: 'none',
        background: 'transparent'
      },
      onClick: function onClick() {
        return handleToggleDisplay(index);
      }
    }, isVisible ? /*#__PURE__*/React.createElement(EyeOutlined, {
      className: "eye-icon eye-icon-visible"
    }) : /*#__PURE__*/React.createElement(EyeInvisibleOutlined, {
      className: "eye-icon eye-icon-hidden"
    }))));
  }, [disabled, getDisplayTitle, handleTitleChange, handleToggleDisplay]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('sortable-wallet-pass-types', className, {
      'sortable-wallet-pass-types-disabled': disabled
    }),
    style: style
  }, /*#__PURE__*/React.createElement(SortableList, {
    dataSource: dataSource,
    onChange: handleSortChange,
    rowKey: "type",
    renderItem: renderItem,
    activationConstraint: {
      distance: 5
    }
  }));
};
export default SortableWalletPassTypes;