import "antd/es/dropdown/style";
import _Dropdown from "antd/es/dropdown";
import "antd/es/menu/style";
import _Menu from "antd/es/menu";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/* eslint-disable no-new-func */
import React from 'react';
import classNames from 'classnames';
import { DownFill } from '@parallel-line/icons';
import { useHistory } from '@parallel-line/hooks';
import { toRoute } from '@parallel-line/common/es/utils';
import { useAccess as getAccess } from '@parallel-line/hooks';
import Button from '../button';

var Action = function Action(_ref) {
  var prefixCls = _ref.prefixCls,
      actions = _ref.actions,
      queryDic = _ref.queryDic,
      record = _ref.record,
      majorWidth = _ref.majorWidth,
      _ref$majorNumber = _ref.majorNumber,
      majorNumber = _ref$majorNumber === void 0 ? 4 : _ref$majorNumber,
      actionRule = _ref.actionRule,
      onActionClick = _ref.onActionClick;
  var majorActions = [];
  var notMajorActions = [];
  var history = useHistory();

  var handleActionClick = function handleActionClick(action) {
    if (action.click) {
      toRoute(history, queryDic, action.click);
    } else onActionClick(action.key || action.name, record);
  };

  var renderActionEvent = function renderActionEvent(action, DefaultAction) {
    var node = onActionClick(action.key || action.name, record, DefaultAction);
    return /*#__PURE__*/React.isValidElement(node) ? node : DefaultAction;
  };

  var internalActionRule = function internalActionRule(action) {
    var hide;
    var major;
    var key = action.key || action.name;
    var accessible = getAccess(action.code);
    if (!accessible) return;

    if (action.hide == null && action.major == null) {
      var _actionRule = actionRule(key, record);

      var _actionRule2 = _slicedToArray(_actionRule, 2);

      hide = _actionRule2[0];
      major = _actionRule2[1];
    } else {
      if (action.hide != null) {
        if (typeof action.hide === 'string') {
          var fn = new Function('record', action.hide.trim().indexOf('return') === 0 ? action.hide : 'return ' + action.hide);
          hide = fn(record);
        } else {
          hide = action.hide;
        }
      } // if (action.major != null) {
      //   if (typeof action.major === 'string') {
      //     const fn = new Function(
      //       'record',
      //       action.major.trim().indexOf('return') === 0
      //         ? action.major
      //         : 'return ' + action.major,
      //     );
      //     major = fn(record);
      //   } else {
      //     major = action.major;
      //   }
      // }

    }

    if (hide) return; // if (major) {
    //   majorActions.push(action);
    // } else {
    //   notMajorActions.push(action);
    // }

    majorActions.push(action);
  };

  actions.forEach(internalActionRule);

  if (majorActions.length > 3) {
    notMajorActions = majorActions.splice(2);
  }

  if ([].concat(_toConsumableArray(majorActions), _toConsumableArray(notMajorActions)).length === 0) return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-actions")
  });

  var renderAction = function renderAction(action, key) {
    return renderActionEvent(action, /*#__PURE__*/React.createElement(Button, {
      type: "link",
      size: "small",
      style: {
        padding: 0
      },
      key: key,
      onClick: function onClick() {
        return handleActionClick(action);
      }
    }, action.name));
  };

  if (majorActions.length === 0) {
    var _notMajorActions = notMajorActions,
        _notMajorActions2 = _toArray(_notMajorActions),
        first = _notMajorActions2[0],
        restActions = _notMajorActions2.slice(1);

    majorActions = [first];
    notMajorActions = _toConsumableArray(restActions);
  }

  var majorClassName = classNames("".concat(prefixCls, "-actions-major"), _defineProperty({}, "".concat(prefixCls, "-actions-major-").concat(majorNumber), !majorWidth));
  if (notMajorActions.length === 0) return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-actions")
  }, /*#__PURE__*/React.createElement("div", {
    className: majorClassName,
    style: {
      width: majorWidth
    }
  }, majorActions.map(renderAction)));
  var menu = /*#__PURE__*/React.createElement(_Menu, null, notMajorActions.map(function (action, key) {
    return renderActionEvent(action, /*#__PURE__*/React.createElement(_Menu.Item, {
      key: key,
      onClick: function onClick() {
        return handleActionClick(action);
      }
    }, /*#__PURE__*/React.createElement(Button, {
      type: "link",
      size: "small"
    }, action.name)));
  }).filter(function (a) {
    return !!a;
  }));
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-actions")
  }, /*#__PURE__*/React.createElement("div", {
    className: majorClassName
  }, majorActions.map(renderAction)), /*#__PURE__*/React.createElement(_Dropdown, {
    overlay: menu
  }, /*#__PURE__*/React.createElement(Button, {
    className: "".concat(prefixCls, "-actions-more"),
    type: "link",
    size: "small"
  }, "\u66F4\u591A", /*#__PURE__*/React.createElement(DownFill, null))));
};

export default Action;