"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireWildcard(require("react"));

var _mobx = require("mobx");

var _mobxReactLite = require("mobx-react-lite");

var _Button = _interopRequireDefault(require("../../../button/Button"));

var _enum = require("../../../button/enum");

var _enum2 = require("../../../core/enum");

var _Dropdown = _interopRequireDefault(require("../../../dropdown/Dropdown"));

var _menu = _interopRequireDefault(require("../../../menu"));

var _Switch = _interopRequireDefault(require("../../../switch/Switch"));

var _TableContext = _interopRequireDefault(require("../../TableContext"));

var _enum3 = require("../../enum");

var _enum4 = require("../../../dropdown/enum");

var _localeContext = require("../../../locale-context");

var Item = _menu["default"].Item;

function findRecords(record, groups) {
  var parent = record.parent;

  if (parent) {
    return parent.children || [];
  }

  var lock = record.get('lock');
  var group = groups.find(function (_ref) {
    var value = _ref.value;
    return value === lock;
  });

  if (group) {
    return group.records;
  }

  return [];
}

var ItemSuffix = function ItemSuffix(props) {
  var record = props.record,
      index = props.index,
      records = props.records,
      groups = props.groups;

  var _useContext = (0, _react.useContext)(_TableContext["default"]),
      prefixCls = _useContext.prefixCls,
      _useContext$tableStor = _useContext.tableStore,
      columnHideable = _useContext$tableStor.columnHideable,
      columnTitleEditable = _useContext$tableStor.columnTitleEditable,
      columnDraggable = _useContext$tableStor.columnDraggable;

  var changeLock = (0, _react.useCallback)(function (lock) {
    var oldLock = record.get('lock');
    var group = groups.find(function (_ref2) {
      var value = _ref2.value;
      return value === lock;
    });

    if (group) {
      record.set('sort', group.records.length);
    }

    record.set('lock', lock);
    var oldGroup = groups.find(function (_ref3) {
      var value = _ref3.value;
      return value === oldLock;
    });

    if (oldGroup) {
      oldGroup.records.filter(function (r) {
        return r !== record;
      }).forEach(function (r, sort) {
        return r.set('sort', sort);
      });
    }
  }, [record, groups]);
  var changeIndex = (0, _react.useCallback)(function (newIndex) {
    var oldSort = record.get('sort');
    var list = findRecords(record, groups);

    var _list$splice = list.splice(oldSort, 1),
        _list$splice2 = (0, _slicedToArray2["default"])(_list$splice, 1),
        removed = _list$splice2[0];

    if (removed) {
      list.splice(newIndex, 0, removed);
    }

    list.forEach(function (r, sort) {
      return r.set('sort', sort);
    });
  }, [record, groups]);
  var handleMenuClick = (0, _react.useCallback)((0, _mobx.action)(function (arg) {
    var key = arg.key;

    switch (key) {
      case 'rename':
        record.setState('editing', true);
        break;

      case 'unlock':
        changeLock(false);
        break;

      case 'left':
        changeLock(_enum3.ColumnLock.left);
        break;

      case 'right':
        changeLock(_enum3.ColumnLock.right);
        break;

      case 'top':
        changeIndex(0);
        break;

      case 'up':
        changeIndex(index - 1);
        break;

      case 'down':
        changeIndex(index + 1);
        break;

      default:
    }
  }), [record, index, changeLock, changeIndex]);

  var getTreeNodesMenus = function getTreeNodesMenus() {
    var lock = record.get('lock');
    var menus = [];

    if (columnTitleEditable && record.get('titleEditable') !== false) {
      menus.push( /*#__PURE__*/_react["default"].createElement(Item, {
        key: "rename"
      }, (0, _localeContext.$l)('Table', 'rename')));
    }

    if (columnDraggable && record.get('draggable') !== false) {
      if (!record.parent) {
        if (lock) {
          menus.push( /*#__PURE__*/_react["default"].createElement(Item, {
            key: "unlock"
          }, (0, _localeContext.$l)('Table', 'unlock')));
        } else {
          menus.push( /*#__PURE__*/_react["default"].createElement(Item, {
            key: "left"
          }, (0, _localeContext.$l)('Table', 'left_lock')), /*#__PURE__*/_react["default"].createElement(Item, {
            key: "right"
          }, (0, _localeContext.$l)('Table', 'right_lock')));
        }
      }

      if (index > 1) {
        menus.push( /*#__PURE__*/_react["default"].createElement(Item, {
          key: 'top'
        }, (0, _localeContext.$l)('Table', 'top')));
      }

      if (index > 0) {
        menus.push( /*#__PURE__*/_react["default"].createElement(Item, {
          key: "up"
        }, (0, _localeContext.$l)('Table', 'up')));
      }

      if (index < records.length - 1) {
        menus.push( /*#__PURE__*/_react["default"].createElement(Item, {
          key: "down"
        }, (0, _localeContext.$l)('Table', 'down')));
      }
    }

    if (menus.length) {
      return /*#__PURE__*/_react["default"].createElement(_menu["default"], {
        prefixCls: "".concat(prefixCls, "-dropdown-menu"),
        onClick: handleMenuClick
      }, menus);
    }
  };

  var menu = getTreeNodesMenus();
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, menu && /*#__PURE__*/_react["default"].createElement(_Dropdown["default"], {
    overlay: menu,
    placement: _enum4.Placements.bottomRight
  }, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    funcType: _enum.FuncType.flat,
    size: _enum2.Size.small,
    icon: "more_horiz",
    className: "".concat(prefixCls, "-customization-tree-treenode-hover-button")
  })), /*#__PURE__*/_react["default"].createElement(_Switch["default"], {
    record: record,
    disabled: !columnHideable || record.get('hideable') === false || record.parent && record.parent.get('hidden'),
    name: "hidden",
    value: false,
    unCheckedValue: true
  }));
};

ItemSuffix.displayName = 'ItemSuffix';

var _default = (0, _mobxReactLite.observer)(ItemSuffix);

exports["default"] = _default;
//# sourceMappingURL=ItemSuffix.js.map
