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 _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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
/**
 * 列显示隐藏弹层内容：显隐切换 + 拖拽排序（参考 table Actions ColumnsSetting PopoverContent）。
 */

import React, { useMemo } from 'react';
import classNames from 'classnames';
import DotsSix from '@pisell/icon/es/DotsSix';
import { EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons';
import { getText } from "../../../../locales";
import Sortable from "../../../DragSortList";
import { getRecordBoardColumnKey } from "../../utils/recordBoardColumns";
var prefix = 'pisell-lowcode-';
function getColumnTitle(column) {
  var title = column.title;
  if (typeof title === 'string') return title;
  return getRecordBoardColumnKey(column) || '';
}
var ColumnSettingPopover = function ColumnSettingPopover(_ref) {
  var columns = _ref.columns,
    columnVisibility = _ref.columnVisibility,
    onColumnVisibilityChange = _ref.onColumnVisibilityChange,
    columnOrder = _ref.columnOrder,
    onColumnOrderChange = _ref.onColumnOrderChange;
  var orderedColumns = useMemo(function () {
    if (!Array.isArray(columnOrder) || columnOrder.length === 0) return columns;
    var keyToCol = new Map(columns.map(function (c) {
      return [getRecordBoardColumnKey(c), c];
    }));
    var result = [];
    var _iterator = _createForOfIteratorHelper(columnOrder),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var key = _step.value;
        var col = keyToCol.get(key);
        if (col) result.push(col);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    columns.forEach(function (col) {
      var key = getRecordBoardColumnKey(col);
      if (!columnOrder.includes(key)) result.push(col);
    });
    return result;
  }, [columns, columnOrder]);
  var handleToggle = function handleToggle(colKey, isShow) {
    onColumnVisibilityChange(_defineProperty({}, colKey, isShow));
  };
  var handleOrderChange = function handleOrderChange(list) {
    onColumnOrderChange === null || onColumnOrderChange === void 0 || onColumnOrderChange(list.map(function (c) {
      return getRecordBoardColumnKey(c);
    }).filter(Boolean));
  };
  var listContent = /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "table-action-column-content")
  }, typeof onColumnOrderChange === 'function' ? /*#__PURE__*/React.createElement(Sortable, {
    list: orderedColumns,
    rowKey: function rowKey(col) {
      return getRecordBoardColumnKey(col) || '';
    },
    onChange: handleOrderChange
  }, function (_ref2) {
    var col = _ref2.item,
      index = _ref2.index,
      style = _ref2.style,
      props = _ref2.props,
      attributes = _ref2.attributes,
      setActivatorNodeRef = _ref2.setActivatorNodeRef,
      listeners = _ref2.listeners;
    var colKey = getRecordBoardColumnKey(col);
    var isShow = columnVisibility[colKey] !== false;
    return /*#__PURE__*/React.createElement("div", _extends({
      key: colKey
    }, attributes, {
      ref: props.ref,
      style: style,
      className: "".concat(prefix, "table-action-column-drag-item")
    }), /*#__PURE__*/React.createElement(DotsSix, _extends({
      ref: setActivatorNodeRef
    }, listeners, {
      style: {
        touchAction: 'none'
      },
      className: "".concat(prefix, "table-action-column-drag-bar")
    })), /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefix, "table-action-column-drag-item-right")
    }, /*#__PURE__*/React.createElement("span", {
      className: classNames("".concat(prefix, "table-action-column-drag-item-right-text"), _defineProperty({}, "".concat(prefix, "table-action-column-drag-item-right-text-hidden"), !isShow))
    }, getColumnTitle(col)), /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefix, "table-action-column-drag-item-right-icon-wrap"),
      onClick: function onClick() {
        return handleToggle(colKey, !isShow);
      },
      role: "button",
      "aria-label": isShow ? 'hide' : 'show'
    }, isShow ? /*#__PURE__*/React.createElement(EyeOutlined, null) : /*#__PURE__*/React.createElement(EyeInvisibleOutlined, null))));
  }) : orderedColumns.map(function (col) {
    var colKey = getRecordBoardColumnKey(col);
    var isShow = columnVisibility[colKey] !== false;
    return /*#__PURE__*/React.createElement("div", {
      key: colKey,
      className: "".concat(prefix, "table-action-column-drag-item"),
      style: {
        cursor: 'default'
      }
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefix, "table-action-column-drag-item-right")
    }, /*#__PURE__*/React.createElement("span", {
      className: classNames("".concat(prefix, "table-action-column-drag-item-right-text"), _defineProperty({}, "".concat(prefix, "table-action-column-drag-item-right-text-hidden"), !isShow))
    }, getColumnTitle(col)), /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefix, "table-action-column-drag-item-right-icon-wrap"),
      onClick: function onClick() {
        return handleToggle(colKey, !isShow);
      },
      role: "button",
      "aria-label": isShow ? 'hide' : 'show'
    }, isShow ? /*#__PURE__*/React.createElement(EyeOutlined, null) : /*#__PURE__*/React.createElement(EyeInvisibleOutlined, null))));
  }));
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "table-action-column-popover-content-wrap")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "table-action-column-header")
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefix, "table-action-column-header-title")
  }, getText('table-action-column-title'))), listContent);
};
export default ColumnSettingPopover;