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

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

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

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

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

var _usePrevious = _interopRequireDefault(require("../utilities/usePrevious"));

var _excluded = ["className", "children", "inline", "showDrawer", "drawerOpen"];

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

/**
 * A link that triggers the visibility of a drawer
 */
var DrawerToggle = function DrawerToggle(_ref) {
  var className = _ref.className,
      children = _ref.children,
      inline = _ref.inline,
      showDrawer = _ref.showDrawer,
      drawerOpen = _ref.drawerOpen,
      others = _objectWithoutProperties(_ref, _excluded);

  var buttonRef = (0, _react.useRef)(null);
  var prevDrawerOpenProp = (0, _usePrevious.default)(drawerOpen);
  (0, _react.useEffect)(function () {
    // if drawer was open but now closed, focus the toggle
    if (prevDrawerOpenProp && !drawerOpen && buttonRef.current) {
      buttonRef.current.focus();
    }
  }, [drawerOpen]);
  var classes = (0, _classnames.default)('ds-c-drawer__toggle', inline && 'ds-c-drawer__toggle--inline', className);
  return /*#__PURE__*/_react.default.createElement(_Button.default, _extends({
    className: classes,
    inputRef: function inputRef(el) {
      return buttonRef.current = el;
    },
    onClick: showDrawer,
    variation: "ghost"
  }, others), children);
};

exports.DrawerToggle = DrawerToggle;
DrawerToggle.propTypes = {
  /**
     * Determines if Drawer is open or closed.
     * This value is used to re-focus the toggle that opened the drawer when the drawer closes.
     */
  drawerOpen: _propTypes.default.bool.isRequired,

  /**
     * DrawerToggle content.
     */
  children: _propTypes.default.node.isRequired,

  /**
     * Additional classes for the toggle button anchor element.
     */
  className: _propTypes.default.string,

  /**
     * Adds `display: inline` to the DrawerToggle.
     */
  inline: _propTypes.default.bool,

  /**
     * This function is called with an id that the toggle generates.
     * It can be used in implementing the Drawer for keeping track of which drawer the toggle controls.
     */
  showDrawer: _propTypes.default.func.isRequired
};
var _default = DrawerToggle;
exports.default = _default;