"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 = exports.UsaBanner = void 0;

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));

var _i18n = require("../i18n");

var _Icons = require("../Icons");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

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

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; }

var UsaBanner = function UsaBanner(props) {
  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      isBannerOpen = _useState2[0],
      setBannerOpen = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      shouldRenderMobileView = _useState4[0],
      setShouldRenderMobileView = _useState4[1];

  var classes = (0, _classnames.default)('ds-c-usa-banner', props.className);
  var id = props.id || (0, _uniqueId.default)('gov-banner_');
  var t = (0, _i18n.tWithLanguage)(props.locale);

  if (props.locale) {
    console.warn("[Deprecated]: Please remove the 'locale' prop in <UsaBanner> in favor of global language setting. This prop is deprecated and will be removed in a future release.");
  }

  (0, _react.useEffect)(function () {
    var media;

    var onMediaChange = function onMediaChange(evt) {
      setShouldRenderMobileView(evt.matches);
    };

    if (window) {
      media = window.matchMedia('(max-width: 543px)');
      media.addEventListener('change', onMediaChange);
      setShouldRenderMobileView(media.matches);
    }

    return function () {
      if (window) {
        media.removeEventListener('change', onMediaChange);
      }
    };
  }, []);

  var toggleBanner = function toggleBanner() {
    setBannerOpen(!isBannerOpen);
  };

  var flagIcon = /*#__PURE__*/_react.default.createElement(_Icons.UsaFlagIcon, {
    className: "ds-c-usa-banner__header-flag",
    title: t('usaBanner.flagIconTitle'),
    ariaHidden: false
  }); // on mobile, the entire header needs to be a clickable element


  var renderMobileHeaderContent = function renderMobileHeaderContent() {
    return /*#__PURE__*/_react.default.createElement("button", {
      onClick: toggleBanner,
      className: "ds-c-usa-banner__button",
      "aria-expanded": isBannerOpen,
      "aria-controls": id
    }, flagIcon, /*#__PURE__*/_react.default.createElement("span", {
      className: "ds-c-usa-banner__header-text"
    }, /*#__PURE__*/_react.default.createElement("span", null, t('usaBanner.bannerText')), !isBannerOpen && /*#__PURE__*/_react.default.createElement("span", {
      className: "ds-c-usa-banner__cta-wrapper"
    }, /*#__PURE__*/_react.default.createElement("span", {
      className: "ds-c-usa-banner__button-text"
    }, t('usaBanner.bannerActionText')), /*#__PURE__*/_react.default.createElement(_Icons.ArrowIcon, {
      direction: "down",
      className: "ds-c-usa-banner__action-icon"
    }))), isBannerOpen && /*#__PURE__*/_react.default.createElement("span", {
      className: "ds-c-usa-banner__collapse-banner-container"
    }, /*#__PURE__*/_react.default.createElement(_Icons.CloseIconThin, null)));
  }; // on larger screens, only cta needs to be clickable


  var renderHeaderContent = function renderHeaderContent() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, flagIcon, /*#__PURE__*/_react.default.createElement("p", {
      className: "ds-c-usa-banner__header-text"
    }, /*#__PURE__*/_react.default.createElement("span", null, t('usaBanner.bannerText')), /*#__PURE__*/_react.default.createElement("button", {
      onClick: toggleBanner,
      className: "ds-c-usa-banner__button ds-c-usa-banner__button-text",
      "aria-expanded": isBannerOpen,
      "aria-controls": id
    }, t('usaBanner.bannerActionText'), /*#__PURE__*/_react.default.createElement(_Icons.ArrowIcon, {
      direction: isBannerOpen ? 'up' : 'down',
      className: "ds-c-usa-banner__action-icon ds-u-margin-left--1"
    }))));
  };

  return /*#__PURE__*/_react.default.createElement("section", {
    className: classes,
    "aria-label": t('usaBanner.bannerLabel')
  }, /*#__PURE__*/_react.default.createElement("header", {
    className: (0, _classnames.default)('ds-c-usa-banner__header', {
      'ds-c-usa-banner__header--expanded': isBannerOpen,
      'ds-c-usa-banner__header--mobile': shouldRenderMobileView
    })
  }, shouldRenderMobileView ? renderMobileHeaderContent() : renderHeaderContent()), /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-usa-banner__content",
    id: id,
    hidden: !isBannerOpen
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-usa-banner__guidance-container"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-usa-banner__guidance"
  }, /*#__PURE__*/_react.default.createElement(_Icons.BuildingCircleIcon, {
    className: "ds-c-usa-banner__icon ds-c-icon-color--primary"
  }), /*#__PURE__*/_react.default.createElement("p", {
    className: "ds-c-usa-banner__media-body"
  }, /*#__PURE__*/_react.default.createElement("strong", null, t('usaBanner.domainHeaderText')), /*#__PURE__*/_react.default.createElement("br", null), t('usaBanner.domainAText'), /*#__PURE__*/_react.default.createElement("strong", null, " ", t('usaBanner.govText'), " "), t('usaBanner.domainText'))), /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-usa-banner__guidance"
  }, /*#__PURE__*/_react.default.createElement(_Icons.LockCircleIcon, {
    className: "ds-c-usa-banner__icon"
  }), /*#__PURE__*/_react.default.createElement("p", {
    className: "ds-c-usa-banner__media-body"
  }, /*#__PURE__*/_react.default.createElement("strong", null, t('usaBanner.httpsHeaderText')), /*#__PURE__*/_react.default.createElement("br", null), t('usaBanner.httpsAText'), /*#__PURE__*/_react.default.createElement("strong", null, " ", t('usaBanner.httpsLockText'), " "), ' ', /*#__PURE__*/_react.default.createElement(_Icons.LockIcon, {
    className: "ds-c-usa-banner__lock-image"
  }), " ", t('usaBanner.httpsOrText'), /*#__PURE__*/_react.default.createElement("strong", null, " ", t('usaBanner.httpsText'), " "), t('usaBanner.httpsDetailText'))))));
};

exports.UsaBanner = UsaBanner;
UsaBanner.propTypes = {
  /**
     * Additional classes to be added to the root `section` element
     */
  className: _propTypes.default.string,

  /**
     * A unique ID to be applied to the banner content. A unique ID will be generated if one isn't provided.
     */
  id: _propTypes.default.string
};
var _default = UsaBanner;
exports.default = _default;