"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

var _consoleComponents = require("@alicloud/console-components");

var _classnames = _interopRequireDefault(require("classnames"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var renderItem = function renderItem(headers) {
  return Array.isArray(headers) ? headers.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Menu.Item, {
      key: item
    }, item);
  }) : null;
};

var Header = function Header(_a) {
  var header = _a.header,
      headers = _a.headers,
      onSelectHeader = _a.onSelectHeader,
      isSildeNav = _a.isSildeNav;

  var _b = (0, _react.useState)('down'),
      direction = _b[0],
      setDirection = _b[1];

  var handleChangeDirection = function handleChangeDirection() {
    setDirection(direction === 'down' ? 'up' : 'down');
  };

  var handleClick = function handleClick(selectedKey) {
    handleChangeDirection();
    onSelectHeader && onSelectHeader(selectedKey);
  };

  return /*#__PURE__*/_react["default"].createElement("h2", {
    className: "lp-menu-header"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "header-text"
  }, header), headers && headers.length > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: "header-select"
  }, /*#__PURE__*/_react["default"].createElement(_consoleComponents.Dropdown, {
    visible: direction === 'up',
    triggerType: "click",
    offset: isSildeNav ? [-175, 10] : [16, 10],
    onVisibleChange: function onVisibleChange(visible) {
      setDirection(visible ? 'up' : 'down');
    },
    container: function container(trigger) {
      return trigger.parentNode;
    },
    cache: true,
    trigger: /*#__PURE__*/_react["default"].createElement("div", {
      className: "trigger-wrap"
    }, /*#__PURE__*/_react["default"].createElement(_consoleComponents.Icon, {
      onClick: handleChangeDirection,
      size: "small",
      type: "sort-" + direction,
      className: (0, _classnames["default"])({
        'trigger-icon': true,
        'icon-up': direction === 'up'
      })
    }))
  }, /*#__PURE__*/_react["default"].createElement(_consoleComponents.Menu, {
    style: {
      left: 0,
      minWidth: '100%'
    },
    selectedKeys: /*#__PURE__*/_react["default"].isValidElement(header) ? [] : header,
    selectMode: "single",
    onItemClick: handleClick
  }, renderItem(headers)))));
};

var _default = Header;
exports["default"] = _default;