import _pt from "prop-types";

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

import React, { useState, useEffect } from 'react';
import classNames from 'classnames';
import uniqueId from 'lodash/uniqueId';
import { tWithLanguage } from '../i18n';
import { LockCircleIcon, LockIcon, UsaFlagIcon, BuildingCircleIcon, ArrowIcon, CloseIconThin } from '../Icons';
export var UsaBanner = function UsaBanner(props) {
  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      isBannerOpen = _useState2[0],
      setBannerOpen = _useState2[1];

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

  var classes = classNames('ds-c-usa-banner', props.className);
  var id = props.id || uniqueId('gov-banner_');
  var t = 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.");
  }

  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.createElement(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.createElement("button", {
      onClick: toggleBanner,
      className: "ds-c-usa-banner__button",
      "aria-expanded": isBannerOpen,
      "aria-controls": id
    }, flagIcon, /*#__PURE__*/React.createElement("span", {
      className: "ds-c-usa-banner__header-text"
    }, /*#__PURE__*/React.createElement("span", null, t('usaBanner.bannerText')), !isBannerOpen && /*#__PURE__*/React.createElement("span", {
      className: "ds-c-usa-banner__cta-wrapper"
    }, /*#__PURE__*/React.createElement("span", {
      className: "ds-c-usa-banner__button-text"
    }, t('usaBanner.bannerActionText')), /*#__PURE__*/React.createElement(ArrowIcon, {
      direction: "down",
      className: "ds-c-usa-banner__action-icon"
    }))), isBannerOpen && /*#__PURE__*/React.createElement("span", {
      className: "ds-c-usa-banner__collapse-banner-container"
    }, /*#__PURE__*/React.createElement(CloseIconThin, null)));
  }; // on larger screens, only cta needs to be clickable


  var renderHeaderContent = function renderHeaderContent() {
    return /*#__PURE__*/React.createElement(React.Fragment, null, flagIcon, /*#__PURE__*/React.createElement("p", {
      className: "ds-c-usa-banner__header-text"
    }, /*#__PURE__*/React.createElement("span", null, t('usaBanner.bannerText')), /*#__PURE__*/React.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.createElement(ArrowIcon, {
      direction: isBannerOpen ? 'up' : 'down',
      className: "ds-c-usa-banner__action-icon ds-u-margin-left--1"
    }))));
  };

  return /*#__PURE__*/React.createElement("section", {
    className: classes,
    "aria-label": t('usaBanner.bannerLabel')
  }, /*#__PURE__*/React.createElement("header", {
    className: classNames('ds-c-usa-banner__header', {
      'ds-c-usa-banner__header--expanded': isBannerOpen,
      'ds-c-usa-banner__header--mobile': shouldRenderMobileView
    })
  }, shouldRenderMobileView ? renderMobileHeaderContent() : renderHeaderContent()), /*#__PURE__*/React.createElement("div", {
    className: "ds-c-usa-banner__content",
    id: id,
    hidden: !isBannerOpen
  }, /*#__PURE__*/React.createElement("div", {
    className: "ds-c-usa-banner__guidance-container"
  }, /*#__PURE__*/React.createElement("div", {
    className: "ds-c-usa-banner__guidance"
  }, /*#__PURE__*/React.createElement(BuildingCircleIcon, {
    className: "ds-c-usa-banner__icon ds-c-icon-color--primary"
  }), /*#__PURE__*/React.createElement("p", {
    className: "ds-c-usa-banner__media-body"
  }, /*#__PURE__*/React.createElement("strong", null, t('usaBanner.domainHeaderText')), /*#__PURE__*/React.createElement("br", null), t('usaBanner.domainAText'), /*#__PURE__*/React.createElement("strong", null, " ", t('usaBanner.govText'), " "), t('usaBanner.domainText'))), /*#__PURE__*/React.createElement("div", {
    className: "ds-c-usa-banner__guidance"
  }, /*#__PURE__*/React.createElement(LockCircleIcon, {
    className: "ds-c-usa-banner__icon"
  }), /*#__PURE__*/React.createElement("p", {
    className: "ds-c-usa-banner__media-body"
  }, /*#__PURE__*/React.createElement("strong", null, t('usaBanner.httpsHeaderText')), /*#__PURE__*/React.createElement("br", null), t('usaBanner.httpsAText'), /*#__PURE__*/React.createElement("strong", null, " ", t('usaBanner.httpsLockText'), " "), ' ', /*#__PURE__*/React.createElement(LockIcon, {
    className: "ds-c-usa-banner__lock-image"
  }), " ", t('usaBanner.httpsOrText'), /*#__PURE__*/React.createElement("strong", null, " ", t('usaBanner.httpsText'), " "), t('usaBanner.httpsDetailText'))))));
};
UsaBanner.propTypes = {
  /**
     * Additional classes to be added to the root `section` element
     */
  className: _pt.string,

  /**
     * A unique ID to be applied to the banner content. A unique ID will be generated if one isn't provided.
     */
  id: _pt.string
};
export default UsaBanner;