'use strict';

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
var react = require('@carbon/react');
var PropTypes = require('prop-types');
var React = require('react');
var Settings = require('../../constants/Settings.js');
var HeaderActionGroup = require('./HeaderActionGroup.js');
var HeaderPropTypes = require('./HeaderPropTypes.js');
var headerConstants = require('./headerConstants.js');
var iconsReact = require('@carbon/icons-react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var React__default = /*#__PURE__*/_interopDefault(React);

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var carbonPrefix = Settings.settings.prefix,
  iotPrefix = Settings.settings.iotPrefix;
var propTypes = {
  /** Add a prefix other than IBM */
  prefix: PropTypes__default.default.string,
  /** Name to follow the IBM prefix up top, left */
  appName: PropTypes__default.default.string.isRequired,
  /** Short name to follow the IBM prefix at top, left on smaller breakpoints */
  // eslint-disable-next-line react/require-default-props, uses appName is none provided
  shortAppName: PropTypes__default.default.string,
  /** Optional prop that provides additional app information */
  subtitle: PropTypes__default.default.oneOfType([PropTypes__default.default.string, PropTypes__default.default.element]),
  /** Add a class name to Header */
  className: PropTypes__default.default.string,
  /** Provide ID for the skip to content functionality */
  skipto: PropTypes__default.default.string,
  /** href optional url to file if you click on title */
  url: PropTypes__default.default.string,
  /** Object of action items */
  actionItems: PropTypes__default.default.arrayOf(PropTypes__default.default.shape(HeaderPropTypes.HeaderActionItemPropTypes)).isRequired,
  /** Bit to flip that tells header to render the nav toggle button */
  hasSideNav: PropTypes__default.default.bool,
  onClickSideNavExpand: PropTypes__default.default.func,
  /** Main app switcher Header panel props */
  headerPanel: PropTypes__default.default.shape(HeaderPropTypes.HeaderPanelPropTypes),
  /** App switcher label */
  appSwitcherLabel: PropTypes__default.default.string,
  i18n: PropTypes__default.default.shape({
    mainHeader: PropTypes__default.default.string,
    openMenu: PropTypes__default.default.string,
    closeMenu: PropTypes__default.default.string
  }),
  testId: PropTypes__default.default.string,
  /** Returns true, if the icon should be shown. (actionItem) => {} */
  isActionItemVisible: PropTypes__default.default.func,
  /** allows setting aria-label on side-nav menu button correctly */
  isSideNavExpanded: PropTypes__default.default.bool,
  /** Make sure the Close icon is always displayed in the HeaderActionPanel action item when the panel is expanded */
  showCloseIconWhenPanelExpanded: PropTypes__default.default.bool,
  /** Optional callback when user clicks on header name */
  handleHeaderNameClick: PropTypes__default.default.func
};

// istanbul ignore next
var defaultProps = {
  onClickSideNavExpand: null,
  hasSideNav: true,
  prefix: 'IBM',
  className: 'main-header',
  skipto: '#main-content',
  headerPanel: null,
  subtitle: null,
  url: '#',
  appSwitcherLabel: headerConstants.APP_SWITCHER,
  i18n: {
    mainHeader: 'main header',
    openMenu: 'Open menu',
    closeMenu: 'Close menu'
  },
  testId: 'header',
  isActionItemVisible: function isActionItemVisible() {
    return true;
  },
  isSideNavExpanded: false,
  showCloseIconWhenPanelExpanded: false,
  handleHeaderNameClick: function handleHeaderNameClick() {
    return null;
  }
};

/**
 * UI header with multiple side panels functionality and dropdowns
 */
var Header = function Header(_ref) {
  var appName = _ref.appName,
    shortAppName = _ref.shortAppName,
    subtitle = _ref.subtitle,
    className = _ref.className,
    actionItemsProp = _ref.actionItems,
    prefix = _ref.prefix,
    skipto = _ref.skipto,
    hasSideNav = _ref.hasSideNav,
    onClickSideNavExpand = _ref.onClickSideNavExpand,
    headerPanel = _ref.headerPanel,
    url = _ref.url,
    appSwitcherLabel = _ref.appSwitcherLabel,
    i18n = _ref.i18n,
    testId = _ref.testId,
    isActionItemVisible = _ref.isActionItemVisible,
    isSideNavExpanded = _ref.isSideNavExpanded,
    showCloseIconWhenPanelExpanded = _ref.showCloseIconWhenPanelExpanded,
    handleHeaderNameClick = _ref.handleHeaderNameClick;
  var mergedI18n = _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  var theShortAppName = shortAppName || appName;
  var actionItems = !headerPanel ? actionItemsProp : [].concat(_toConsumableArray__default.default(actionItemsProp), [{
    id: 'app-switcher',
    label: appSwitcherLabel,
    hasHeaderPanel: true,
    btnContent: /*#__PURE__*/React__default.default.createElement(iconsReact.Switcher, {
      size: 20,
      fill: "white",
      className: "".concat(carbonPrefix, "--header__menu-item ").concat(carbonPrefix, "--header__menu-title")
    }),
    childContent: [{
      metaData: {
        className: "".concat(carbonPrefix, "--app-switcher ").concat(headerPanel.className),
        element: 'a'
      },
      content: /*#__PURE__*/React__default.default.createElement(headerPanel.content, null)
    }]
  }]);
  return /*#__PURE__*/React__default.default.createElement(react.Header, {
    "data-testid": testId,
    className: className,
    "aria-label": mergedI18n.mainHeader
  }, /*#__PURE__*/React__default.default.createElement(react.SkipToContent, {
    href: skipto
  }), hasSideNav && /*#__PURE__*/React__default.default.createElement(react.HeaderMenuButton, {
    "data-testid": "".concat(testId, "-menu-button"),
    "aria-label": isSideNavExpanded ? mergedI18n.closeMenu : mergedI18n.openMenu,
    onClick: onClickSideNavExpand,
    isActive: isSideNavExpanded
  }), /*#__PURE__*/React__default.default.createElement(react.HeaderName, {
    "data-testid": "".concat(testId, "-name"),
    href: url,
    onClick: handleHeaderNameClick,
    prefix: prefix
  }, /*#__PURE__*/React__default.default.createElement("span", null, appName), theShortAppName ? /*#__PURE__*/React__default.default.createElement("span", {
    className: "".concat(iotPrefix, "--header__short-name")
  }, theShortAppName) : null, subtitle ? /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--header__subtitle")
  }, subtitle) : null), /*#__PURE__*/React__default.default.createElement(HeaderActionGroup, {
    actionItems: actionItems,
    i18n: mergedI18n,
    testId: "".concat(testId, "-action-group"),
    isActionItemVisible: isActionItemVisible,
    showCloseIconWhenPanelExpanded: showCloseIconWhenPanelExpanded
  }));
};
Header.propTypes = propTypes;
Header.defaultProps = defaultProps;
Header.__docgenInfo = {
  "description": "UI header with multiple side panels functionality and dropdowns",
  "methods": [],
  "displayName": "Header",
  "props": {
    "onClickSideNavExpand": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "hasSideNav": {
      "defaultValue": {
        "value": "true",
        "computed": false
      },
      "description": "Bit to flip that tells header to render the nav toggle button",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "prefix": {
      "defaultValue": {
        "value": "'IBM'",
        "computed": false
      },
      "description": "Add a prefix other than IBM",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "className": {
      "defaultValue": {
        "value": "'main-header'",
        "computed": false
      },
      "description": "Add a class name to Header",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "skipto": {
      "defaultValue": {
        "value": "'#main-content'",
        "computed": false
      },
      "description": "Provide ID for the skip to content functionality",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "headerPanel": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Main app switcher Header panel props",
      "type": {
        "name": "shape",
        "value": "import { HeaderActionItemPropTypes, HeaderPanelPropTypes } from './HeaderPropTypes';",
        "computed": true
      },
      "required": false
    },
    "subtitle": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Optional prop that provides additional app information",
      "type": {
        "name": "union",
        "value": [{
          "name": "string"
        }, {
          "name": "element"
        }]
      },
      "required": false
    },
    "url": {
      "defaultValue": {
        "value": "'#'",
        "computed": false
      },
      "description": "href optional url to file if you click on title",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "appSwitcherLabel": {
      "defaultValue": {
        "value": "APP_SWITCHER",
        "computed": true
      },
      "description": "App switcher label",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  mainHeader: 'main header',\n  openMenu: 'Open menu',\n  closeMenu: 'Close menu',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "mainHeader": {
            "name": "string",
            "required": false
          },
          "openMenu": {
            "name": "string",
            "required": false
          },
          "closeMenu": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'header'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "isActionItemVisible": {
      "defaultValue": {
        "value": "() => true",
        "computed": false
      },
      "description": "Returns true, if the icon should be shown. (actionItem) => {}",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "isSideNavExpanded": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "allows setting aria-label on side-nav menu button correctly",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "showCloseIconWhenPanelExpanded": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Make sure the Close icon is always displayed in the HeaderActionPanel action item when the panel is expanded",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "handleHeaderNameClick": {
      "defaultValue": {
        "value": "() => null",
        "computed": false
      },
      "description": "Optional callback when user clicks on header name",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "appName": {
      "description": "Name to follow the IBM prefix up top, left",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "shortAppName": {
      "description": "Short name to follow the IBM prefix at top, left on smaller breakpoints",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "actionItems": {
      "description": "Object of action items",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": "import { HeaderActionItemPropTypes, HeaderPanelPropTypes } from './HeaderPropTypes';",
          "computed": true
        }
      },
      "required": true
    }
  }
};
var Header$1 = Header;

module.exports = Header$1;
