"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

/*
 * Copyright (C) 2018 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/*eslint no-console: 0*/
var focusedEditor = null;
var resolveEditorRendered;
var Bridge = {
  activeEditor: function activeEditor() {
    return focusedEditor;
  },
  focusEditor: function focusEditor(editor) {
    focusedEditor = editor;
  },
  detachEditor: function detachEditor(editor) {
    if (editor === focusedEditor) {
      focusedEditor = null;
    }
  },
  getEditor: function getEditor() {
    return focusedEditor;
  },
  renderEditor: function renderEditor(editor) {
    resolveEditorRendered();

    if (focusedEditor === null) {
      focusedEditor = editor;
    }
  },
  editorRendered: new Promise(function (resolve) {
    resolveEditorRendered = resolve;
  }),
  existingContentToLink: function existingContentToLink() {
    if (focusedEditor) {
      return focusedEditor.existingContentToLink();
    }

    return false;
  },
  existingContentToLinkIsImg: function existingContentToLinkIsImg() {
    if (focusedEditor) {
      return focusedEditor.existingContentToLinkIsImg();
    }

    return false;
  },
  insertLink: function insertLink(link) {
    if (focusedEditor) {
      var _focusedEditor$props$ = focusedEditor.props.tinymce.get(focusedEditor.props.textareaId),
          selection = _focusedEditor$props$.selection;

      link.selectionDetails = {
        node: selection.getNode(),
        range: selection.getRng()
      };
      focusedEditor.insertLink(link);
    } else {
      console.warn("clicked sidebar link without a focused editor");
    }
  },
  insertImage: function insertImage(image) {
    if (focusedEditor) {
      focusedEditor.insertImage(image);
    } else {
      console.warn("clicked sidebar image without a focused editor");
    }
  },
  embedImage: function embedImage(image) {
    if (Bridge.existingContentToLink() && !Bridge.existingContentToLinkIsImg()) {
      Bridge.insertLink({
        title: image.display_name,
        href: image.href,
        embed: {
          type: "image"
        }
      });
    } else {
      Bridge.insertImage(image);
    }
  }
};
var _default = Bridge;
exports.default = _default;