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, { useCallback, useMemo } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { injectIntl } from 'react-intl';
import { HeaderButtons } from '@choerodon/master';
import map from 'lodash/map';
import filter from 'lodash/filter';
import { developReportList, reportListMap } from "../Home/reportList";

var ReportsHeaderButtons = function ReportsHeaderButtons(_ref) {
  var refresh = _ref.refresh,
      backPath = _ref.backPath,
      reportType = _ref.reportType,
      reportKey = _ref.reportKey,
      formatMessage = _ref.intl.formatMessage,
      _ref$extraItems = _ref.extraItems,
      extraItems = _ref$extraItems === void 0 ? [] : _ref$extraItems;
  var history = useHistory();

  var _useLocation = useLocation(),
      search = _useLocation.search;

  var realBackPath = useMemo(function () {
    return backPath || "/devops/charts/".concat(reportType).concat(search);
  }, [backPath, search, reportType]);
  var reportList = useMemo(function () {
    return reportListMap[reportType] || developReportList;
  }, [reportType]);
  var groupBtnItems = useMemo(function () {
    return map(filter(reportList, function (item) {
      return (item === null || item === void 0 ? void 0 : item.key) !== reportKey;
    }), function (reportItem) {
      return {
        // @ts-ignore
        name: (reportItem === null || reportItem === void 0 ? void 0 : reportItem.title) || '',
        handler: function handler() {
          return handleClick(reportItem === null || reportItem === void 0 ? void 0 : reportItem.link);
        }
      };
    });
  }, [reportList]);
  var handleRefresh = useCallback(function () {
    refresh && refresh();
  }, []);
  var handleBack = useCallback(function () {
    history.push(realBackPath);
  }, [realBackPath]);
  var handleClick = useCallback(function (link) {
    if (link) {
      history.push("".concat(link).concat(search));
    }
  }, [search]);
  return /*#__PURE__*/React.createElement(HeaderButtons, {
    items: [{
      name: '切换报表',
      groupBtnItems: groupBtnItems,
      display: true
    }].concat(_toConsumableArray(extraItems || []), [{
      icon: 'arrow_back',
      handler: handleBack,
      display: true
    }, {
      icon: 'refresh',
      handler: handleRefresh,
      display: true
    }])
  });
};

export default injectIntl(ReportsHeaderButtons);