"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 _analyticsNext = require("@atlaskit/analytics-next");
var _linkPicker = require("@atlaskit/link-picker");
var _utils = require("../../../utils");
var _useEscapeClickaway = require("./useEscapeClickaway");
var _excluded = ["view", "onCancel", "invokeMethod", "editorAppearance", "onClose", "onEscapeCallback", "onClickAwayCallback"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * 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]);
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: ref
  }, /*#__PURE__*/_react.default.createElement(_analyticsNext.AnalyticsContext, {
    data: analyticsData
  }, /*#__PURE__*/_react.default.createElement(_linkPicker.LinkPicker, (0, _extends2.default)({}, restProps, {
    onCancel: onEscape
  }))));
};