"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EditorLinkPicker = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactFocusLock = _interopRequireDefault(require("react-focus-lock"));
var _AnalyticsContext = _interopRequireDefault(require("@atlaskit/analytics-next/AnalyticsContext"));
var _lazy = require("@atlaskit/link-picker/lazy");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _utils = require("../../../utils");
var _useEscapeClickaway = require("./useEscapeClickaway");
var _excluded = ["view", "onCancel", "invokeMethod", "editorAppearance", "onClose", "onEscapeCallback", "onClickAwayCallback"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
 * Returns a type that matches T but where keys (K) are now optional
 */

var EditorLinkPicker = exports.EditorLinkPicker = function EditorLinkPicker(_ref) {
  var view = _ref.view,
    onCancel = _ref.onCancel,
    _ref$invokeMethod = _ref.invokeMethod,
    invokeMethod = _ref$invokeMethod === void 0 ? '_unknown' : _ref$invokeMethod,
    editorAppearance = _ref.editorAppearance,
    onClose = _ref.onClose,
    onEscapeCallback = _ref.onEscapeCallback,
    onClickAwayCallback = _ref.onClickAwayCallback,
    restProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  /**
   * Track onClose handler in a
   * ref so that we void needing it in the dependency array
   * below
   */
  var onCloseRef = (0, _react.useRef)(onClose);
  (0, _react.useEffect)(function () {
    onCloseRef.current = onClose;
  });

  /**
   * Call onClose on mount, usefull to provide
   * a handler for performing an action after the component has been
   * unmounted (e.g. return focus to the editors)
   */
  (0, _react.useEffect)(function () {
    return function () {
      var _onCloseRef$current;
      return (_onCloseRef$current = onCloseRef.current) === null || _onCloseRef$current === void 0 ? void 0 : _onCloseRef$current.call(onCloseRef);
    };
  }, []);
  var onEscape = (0, _react.useCallback)(function () {
    var state = view.state,
      dispatch = view.dispatch;
    onEscapeCallback === null || onEscapeCallback === void 0 || onEscapeCallback(state, dispatch);
    onCancel === null || onCancel === void 0 || onCancel();
  }, [view, onCancel, onEscapeCallback]);
  var onClickAway = (0, _react.useCallback)(function () {
    var state = view.state,
      dispatch = view.dispatch;
    onClickAwayCallback === null || onClickAwayCallback === void 0 || onClickAwayCallback(state, dispatch);
    onCancel === null || onCancel === void 0 || onCancel();
  }, [view, onCancel, onClickAwayCallback]);
  var ref = (0, _useEscapeClickaway.useEscapeClickaway)(onEscape, onClickAway);
  var analyticsEditorAppearance = (0, _utils.getAnalyticsEditorAppearance)(editorAppearance);
  var analyticsData = (0, _react.useMemo)(function () {
    return {
      attributes: {
        invokeMethod: invokeMethod,
        location: analyticsEditorAppearance
      },
      // Below is added for the future implementation of Linking Platform namespaced analytic context
      location: analyticsEditorAppearance
    };
  }, [invokeMethod, analyticsEditorAppearance]);
  if ((0, _expValEquals.expValEquals)('platform_editor_a11y_escape_link_dialog', 'isEnabled', true)) {
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: ref
    }, /*#__PURE__*/_react.default.createElement(_AnalyticsContext.default, {
      data: analyticsData
    }, /*#__PURE__*/_react.default.createElement(_reactFocusLock.default, {
      returnFocus: {
        preventScroll: true
      },
      focusOptions: {
        preventScroll: true
      }
    }, /*#__PURE__*/_react.default.createElement(_lazy.LazyLinkPicker
    // Ignored via go/ees005
    // eslint-disable-next-line react/jsx-props-no-spreading
    , (0, _extends2.default)({}, restProps, {
      onCancel: onEscape
    })))));
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: ref
  }, /*#__PURE__*/_react.default.createElement(_AnalyticsContext.default, {
    data: analyticsData
  }, /*#__PURE__*/_react.default.createElement(_lazy.LazyLinkPicker
  // Ignored via go/ees005
  // eslint-disable-next-line react/jsx-props-no-spreading
  , (0, _extends2.default)({}, restProps, {
    onCancel: onEscape
  }))));
};