import _pt from "prop-types";
var _excluded = ["analytics", "analyticsLabelOverride", "children", "className"];

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

import { EventCategory, MAX_LENGTH, sendLinkEvent } from '../analytics';
import React, { useEffect, useRef } from 'react';
import Drawer from '../Drawer/Drawer';
import { helpDrawerSendsAnalytics } from '../flags';
import classNames from 'classnames';
export var HelpDrawer = function HelpDrawer(props) {
  var analytics = props.analytics,
      analyticsLabelOverride = props.analyticsLabelOverride,
      children = props.children,
      className = props.className,
      others = _objectWithoutProperties(props, _excluded);

  var headingRef = 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.");
    }
  }

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

    if (!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, MAX_LENGTH)) !== null && _ref !== void 0 ? _ref : ''; // Send analytics event for helpdrawer open

    sendLinkEvent({
      event_name: 'help_drawer_opened',
      event_type: EventCategory.UI_INTERACTION,
      ga_eventAction: 'opened help drawer',
      ga_eventCategory: EventCategory.UI_COMPONENTS,
      ga_eventLabel: eventHeadingText,
      heading: eventHeadingText
    });
    return function () {
      // Send analytics event for helpdrawer open
      sendLinkEvent({
        event_name: 'help_drawer_opened',
        event_type: EventCategory.UI_INTERACTION,
        ga_eventAction: 'opened help drawer',
        ga_eventCategory: EventCategory.UI_COMPONENTS,
        ga_eventLabel: eventHeadingText,
        heading: eventHeadingText
      });
    };
  }, []);
  return /*#__PURE__*/React.createElement(Drawer, _extends({
    className: classNames(className, 'ds-c-help-drawer'),
    headingRef: headingRef
  }, others), children);
};
HelpDrawer.propTypes = {
  /**
     * Analytics events tracking is enabled by default. Set this value to `false` to disable tracking for this component instance.
     */
  analytics: _pt.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: _pt.string,

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