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

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

var _Button = _interopRequireDefault(require("../Button/Button"));

var _NativeDialog = _interopRequireDefault(require("../NativeDialog/NativeDialog"));

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

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

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

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

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

var Drawer = function Drawer(props) {
  var _props$ariaLabel, _props$closeButtonTex;

  var headingRef = (0, _react.useRef)(null);
  var id = (0, _react.useRef)(props.headingId || (0, _uniqueId.default)('drawer_'));
  var Heading = "h".concat(props.headingLevel);
  return /*#__PURE__*/_react.default.createElement(_NativeDialog.default, {
    "aria-labelledby": id.current,
    className: (0, _classnames.default)(props.className, 'ds-c-drawer'),
    exit: props.onCloseClick,
    showModal: props.hasFocusTrap
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-drawer__window"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-drawer__header"
  }, /*#__PURE__*/_react.default.createElement(Heading, {
    tabIndex: 0,
    id: id.current,
    className: "ds-c-drawer__header-heading",
    ref: function ref(el) {
      headingRef.current = el;

      if (props.headingRef) {
        props.headingRef.current = el;
      }
    }
  }, props.heading), /*#__PURE__*/_react.default.createElement(_Button.default, {
    "aria-label": (_props$ariaLabel = props.ariaLabel) !== null && _props$ariaLabel !== void 0 ? _props$ariaLabel : (0, _i18n.t)('drawer.ariaLabel'),
    className: "ds-c-drawer__close-button ds-u-fill--white",
    size: "small",
    onClick: props.onCloseClick
  }, (_props$closeButtonTex = props.closeButtonText) !== null && _props$closeButtonTex !== void 0 ? _props$closeButtonTex : (0, _i18n.t)('drawer.closeButtonText'))), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)('ds-c-drawer__body', {
      'ds-c-drawer--is-sticky': props.isHeaderSticky || props.isFooterSticky
    })
  }, props.children), /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-drawer__footer"
  }, /*#__PURE__*/_react.default.createElement("h4", {
    className: "ds-c-drawer__footer-title"
  }, props.footerTitle), /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-drawer__footer-body"
  }, props.footerBody))));
};

exports.Drawer = Drawer;
Drawer.propTypes = {
  /**
     * Gives more context to screen readers on the Drawer close button.
     */
  ariaLabel: _propTypes.default.string,
  closeButtonText: _propTypes.default.node,
  children: _propTypes.default.node.isRequired,
  className: _propTypes.default.string,
  footerBody: _propTypes.default.node,
  footerTitle: _propTypes.default.string,

  /**
     * Enables focus trap functionality within Drawer.
     */
  hasFocusTrap: _propTypes.default.bool,

  /**
     * Text for the Drawer heading. Required because the `heading` will be focused on mount.
     */
  heading: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.node]).isRequired,

  /**
     * A unique `id` to be used on heading element to label multiple instances of Drawer.
     */
  headingId: _propTypes.default.string,

  /**
     * Heading type to override default `<h3>`
     */
  headingLevel: _propTypes.default.oneOf(['1', '2', '3', '4', '5']),

  /**
     * Enables "sticky" position of Drawer header element.
     */
  isHeaderSticky: _propTypes.default.bool,

  /**
     * Enables "sticky" position of Drawer footer element.
     */
  isFooterSticky: _propTypes.default.bool,
  onCloseClick: _propTypes.default.func.isRequired
};
Drawer.defaultProps = {
  headingLevel: '3',
  hasFocusTrap: false
};
var _default = Drawer;
exports.default = _default;