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

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

var _analytics = require("../analytics");

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

var _Drawer = _interopRequireDefault(require("../Drawer/Drawer"));

var _flags = require("../flags");

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

var _excluded = ["analytics", "analyticsLabelOverride", "children", "className"];

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

var HelpDrawer = function HelpDrawer(props) {
  var analytics = props.analytics,
      analyticsLabelOverride = props.analyticsLabelOverride,
      children = props.children,
      className = props.className,
      others = _objectWithoutProperties(props, _excluded);

  var headingRef = (0, _react.useRef)();

  if (process.env.NODE_ENV !== 'production') {
    if (props.title) {
      console.warn("[Deprecated]: Please remove the 'title' prop in <HelpDrawer>, use 'heading' instead. This prop has been renamed and will be removed in a future release.");
    }

    if (!props.title && !props.heading) {
      console.warn("The 'heading' prop in <HelpDrawer> is required. The 'title' prop has been renamed to 'heading' and will be removed in a future release.");
    }
  }

  (0, _react.useEffect)(function () {
    var _ref, _props$analyticsLabel, _headingRef$current, _headingRef$current$t;

    if (!(0, _flags.helpDrawerSendsAnalytics)() || analytics === false) {
      return;
    }

    var eventHeadingText = (_ref = (_props$analyticsLabel = props.analyticsLabelOverride) !== null && _props$analyticsLabel !== void 0 ? _props$analyticsLabel : (_headingRef$current = headingRef.current) === null || _headingRef$current === void 0 ? void 0 : (_headingRef$current$t = _headingRef$current.textContent) === null || _headingRef$current$t === void 0 ? void 0 : _headingRef$current$t.substring(0, _analytics.MAX_LENGTH)) !== null && _ref !== void 0 ? _ref : ''; // Send analytics event for helpdrawer open

    (0, _analytics.sendLinkEvent)({
      event_name: 'help_drawer_opened',
      event_type: _analytics.EventCategory.UI_INTERACTION,
      ga_eventAction: 'opened help drawer',
      ga_eventCategory: _analytics.EventCategory.UI_COMPONENTS,
      ga_eventLabel: eventHeadingText,
      heading: eventHeadingText
    });
    return function () {
      // Send analytics event for helpdrawer open
      (0, _analytics.sendLinkEvent)({
        event_name: 'help_drawer_opened',
        event_type: _analytics.EventCategory.UI_INTERACTION,
        ga_eventAction: 'opened help drawer',
        ga_eventCategory: _analytics.EventCategory.UI_COMPONENTS,
        ga_eventLabel: eventHeadingText,
        heading: eventHeadingText
      });
    };
  }, []);
  return /*#__PURE__*/_react.default.createElement(_Drawer.default, _extends({
    className: (0, _classnames.default)(className, 'ds-c-help-drawer'),
    headingRef: headingRef
  }, others), children);
};

exports.HelpDrawer = HelpDrawer;
HelpDrawer.propTypes = {
  /**
     * Analytics events tracking is enabled by default. Set this value to `false` to disable tracking for this component instance.
     */
  analytics: _propTypes.default.bool,

  /**
     * An override for the dynamic content sent to analytics services. By default this content comes from the heading.
     *
     * In cases where this component’s heading may contain **sensitive information**, use this prop to override what is sent to analytics.
     */
  analyticsLabelOverride: _propTypes.default.string,

  /**
     * @hide-prop [Deprecated] This prop has been renamed to `heading`.
     */
  title: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.node])
};
var _default = HelpDrawer;
exports.default = _default;