'use strict';

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
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.array.map.js');
require('core-js/modules/es.function.name.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 _regeneratorRuntime = require('@babel/runtime/regenerator');
var React = require('react');
var PropTypes = require('prop-types');
var Settings = require('../../../constants/Settings.js');
var Button = require('../../Button/Button.js');
var suiteHeaderUtils = require('../suiteHeaderUtils.js');
var suiteHeaderConstants = require('../suiteHeaderConstants.js');
var SuiteHeaderPropTypes = require('../SuiteHeaderPropTypes.js');
var componentUtilityFunctions = require('../../../utils/componentUtilityFunctions.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 _asyncToGenerator__default = /*#__PURE__*/_interopDefault(_asyncToGenerator);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

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 defaultProps = {
  applications: null,
  customApplications: [],
  allApplicationsLink: null,
  onRouteChange: function () {
    var _onRouteChange = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee() {
      return _regeneratorRuntime__default.default.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", true);
          case 1:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    function onRouteChange() {
      return _onRouteChange.apply(this, arguments);
    }
    return onRouteChange;
  }(),
  i18n: {
    myApplications: 'My applications',
    allApplicationsLink: 'All applications',
    requestAccess: 'Contact your administrator to request application access.',
    learnMoreLink: 'Learn more'
  },
  testId: 'suite-header-app-switcher',
  isExpanded: false
};
var propTypes = {
  applications: PropTypes__default.default.arrayOf(PropTypes__default.default.shape(SuiteHeaderPropTypes.SuiteHeaderApplicationPropTypes)),
  customApplications: PropTypes__default.default.arrayOf(PropTypes__default.default.shape(SuiteHeaderPropTypes.SuiteHeaderApplicationPropTypes)),
  allApplicationsLink: PropTypes__default.default.string,
  noAccessLink: PropTypes__default.default.string.isRequired,
  onRouteChange: PropTypes__default.default.func,
  i18n: PropTypes__default.default.shape({
    allApplicationsLink: PropTypes__default.default.string,
    requestAccess: PropTypes__default.default.string,
    learnMoreLink: PropTypes__default.default.string
  }),
  testId: PropTypes__default.default.string,
  isExpanded: PropTypes__default.default.bool
};
var SuiteHeaderAppSwitcher = function SuiteHeaderAppSwitcher(_ref) {
  var applications = _ref.applications,
    customApplications = _ref.customApplications,
    allApplicationsLink = _ref.allApplicationsLink,
    noAccessLink = _ref.noAccessLink,
    i18n = _ref.i18n,
    onRouteChange = _ref.onRouteChange,
    testId = _ref.testId,
    isExpanded = _ref.isExpanded;
  var mergedI18n = _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  var baseClassName = "".concat(Settings.settings.iotPrefix, "--suite-header-app-switcher");
  var mergedApplications = applications ? [].concat(_toConsumableArray__default.default(customApplications), _toConsumableArray__default.default(applications)) : customApplications.length > 0 ? _toConsumableArray__default.default(customApplications) : null;
  var handleRouteChange = React.useCallback(function (_ref2) {
    var href = _ref2.href,
      id = _ref2.id,
      isExternal = _ref2.isExternal;
    return /*#__PURE__*/function () {
      var _ref3 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee2(e) {
        var newWindow, result;
        return _regeneratorRuntime__default.default.wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              e.preventDefault();
              newWindow = suiteHeaderUtils.shouldOpenInNewWindow(e);
              _context2.next = 4;
              return onRouteChange(suiteHeaderConstants.SUITE_HEADER_ROUTE_TYPES.APPLICATION, href, {
                appId: id
              });
            case 4:
              result = _context2.sent;
              if (result) {
                if (isExternal || newWindow) {
                  window.open(href, '_blank', 'noopener noreferrer');
                } else {
                  window.location.href = href;
                }
              }
            case 6:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x) {
        return _ref3.apply(this, arguments);
      };
    }();
  }, [onRouteChange]);
  var handleAllApplicationRoute = React.useCallback(/*#__PURE__*/function () {
    var _ref4 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee3(e) {
      var newWindow, result;
      return _regeneratorRuntime__default.default.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            e.preventDefault();
            newWindow = suiteHeaderUtils.shouldOpenInNewWindow(e);
            _context3.next = 4;
            return onRouteChange(suiteHeaderConstants.SUITE_HEADER_ROUTE_TYPES.NAVIGATOR, allApplicationsLink);
          case 4:
            result = _context3.sent;
            if (result) {
              if (newWindow) {
                window.open(allApplicationsLink, '_blank', 'noopener noreferrer');
              } else {
                window.location.href = allApplicationsLink;
              }
            }
          case 6:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    return function (_x2) {
      return _ref4.apply(this, arguments);
    };
  }(), [allApplicationsLink, onRouteChange]);
  var tabIndex = isExpanded ? 0 : -1;
  return /*#__PURE__*/React__default.default.createElement("ul", {
    "data-testid": testId,
    className: baseClassName,
    tabIndex: tabIndex
  }, /*#__PURE__*/React__default.default.createElement("li", {
    className: "".concat(baseClassName, "--nav-link")
  }, allApplicationsLink ? /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, /*#__PURE__*/React__default.default.createElement("p", null, mergedI18n.myApplications), /*#__PURE__*/React__default.default.createElement(Button, {
    kind: "tertiary",
    testId: "".concat(testId, "--all-applications"),
    onClick: handleAllApplicationRoute,
    onKeyDown: componentUtilityFunctions.handleSpecificKeyDown(['Enter', 'Space'], handleAllApplicationRoute),
    renderIcon: iconsReact.ArrowRight,
    tabIndex: tabIndex,
    href: allApplicationsLink,
    rel: "noopener noreferrer",
    className: "".concat(baseClassName, "--nav-link--button-tertiary")
  }, mergedI18n.allApplicationsLink)) : null), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--nav-link--separator")
  }), mergedApplications === null || mergedApplications === void 0 ? void 0 : mergedApplications.map(function (_ref5) {
    var id = _ref5.id,
      name = _ref5.name,
      href = _ref5.href,
      _ref5$isExternal = _ref5.isExternal,
      isExternal = _ref5$isExternal === void 0 ? false : _ref5$isExternal;
    var eventHandler = handleRouteChange({
      href: href,
      id: id,
      isExternal: isExternal
    });
    return /*#__PURE__*/React__default.default.createElement("li", {
      id: "suite-header-application-".concat(id),
      key: "key-".concat(id),
      className: "".concat(baseClassName, "--app-link")
    }, /*#__PURE__*/React__default.default.createElement(Button, {
      kind: "ghost",
      testId: "".concat(testId, "--").concat(id),
      onClick: eventHandler,
      onKeyDown: componentUtilityFunctions.handleSpecificKeyDown(['Enter', 'Space'], eventHandler),
      tabIndex: tabIndex,
      href: href,
      rel: "noopener noreferrer"
    }, name));
  }), (mergedApplications === null || mergedApplications === void 0 ? void 0 : mergedApplications.length) === 0 ? /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--no-app"),
    role: "listitem"
  }, /*#__PURE__*/React__default.default.createElement("div", {
    className: "bee-icon-container"
  }, /*#__PURE__*/React__default.default.createElement(iconsReact.Bee, {
    size: 32
  }), /*#__PURE__*/React__default.default.createElement("div", {
    className: "bee-shadow"
  })), /*#__PURE__*/React__default.default.createElement("span", null, mergedI18n.requestAccess), /*#__PURE__*/React__default.default.createElement("a", {
    href: noAccessLink,
    rel: "noopener noreferrer",
    "data-testid": "suite-header-app-switcher--no-access",
    onClick: (/*#__PURE__*/function () {
      var _ref6 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee4(e) {
        var result;
        return _regeneratorRuntime__default.default.wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              e.preventDefault();
              _context4.next = 3;
              return onRouteChange(suiteHeaderConstants.SUITE_HEADER_ROUTE_TYPES.DOCUMENTATION, noAccessLink);
            case 3:
              result = _context4.sent;
              if (result) {
                window.location.href = noAccessLink;
              }
            case 5:
            case "end":
              return _context4.stop();
          }
        }, _callee4);
      }));
      return function (_x3) {
        return _ref6.apply(this, arguments);
      };
    }()),
    tabIndex: tabIndex
  }, mergedI18n.learnMoreLink)) : null);
};
SuiteHeaderAppSwitcher.defaultProps = defaultProps;
SuiteHeaderAppSwitcher.propTypes = propTypes;
SuiteHeaderAppSwitcher.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "SuiteHeaderAppSwitcher",
  "props": {
    "applications": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": "import { SuiteHeaderApplicationPropTypes } from '../SuiteHeaderPropTypes';",
          "computed": true
        }
      },
      "required": false
    },
    "customApplications": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": "import { SuiteHeaderApplicationPropTypes } from '../SuiteHeaderPropTypes';",
          "computed": true
        }
      },
      "required": false
    },
    "allApplicationsLink": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onRouteChange": {
      "defaultValue": {
        "value": "async () => true",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  myApplications: 'My applications',\n  allApplicationsLink: 'All applications',\n  requestAccess: 'Contact your administrator to request application access.',\n  learnMoreLink: 'Learn more',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "allApplicationsLink": {
            "name": "string",
            "required": false
          },
          "requestAccess": {
            "name": "string",
            "required": false
          },
          "learnMoreLink": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'suite-header-app-switcher'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "isExpanded": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "noAccessLink": {
      "description": "",
      "type": {
        "name": "string"
      },
      "required": true
    }
  }
};
var SuiteHeaderAppSwitcher$1 = SuiteHeaderAppSwitcher;

module.exports = SuiteHeaderAppSwitcher$1;
