"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderLink = renderLink;
exports.renderLinkedImage = renderLinkedImage;
exports.constructJSXImageElement = constructJSXImageElement;
exports.renderImage = renderImage;

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

var _server = require("react-dom/server");

var _formatMessage = _interopRequireDefault(require("../format-message"));

/*
 * 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/>.
 */
function renderLink(link) {
  var href = link.href || link.url;
  var title = link.title || "\u0421\u0441\u044B\u043B\u043A\u0430";
  var previewAlt = link["data-preview-alt"];
  var contents = link.contents || title;
  return (0, _server.renderToStaticMarkup)(_react.default.createElement("a", {
    href: href,
    title: title,
    "data-preview-alt": previewAlt,
    className: link["class"],
    id: link["id"]
  }, contents));
}

function renderLinkedImage(linkElem, image) {
  var linkHref = linkElem.getAttribute("href");
  return (0, _server.renderToStaticMarkup)(_react.default.createElement("a", {
    href: linkHref,
    "data-mce-href": linkHref
  }, constructJSXImageElement(image, {
    doNotLink: true
  })));
}

function constructJSXImageElement(image) {
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var href = image.href || image.url;

  var ret = _react.default.createElement("img", {
    alt: image.title || image.display_name,
    src: href
  });

  if (image.alt_text) {
    if (image.alt_text.decorativeSelected) {
      ret = _react.default.createElement("img", {
        alt: "",
        "data-decorative": "true",
        src: href
      });
    } else {
      ret = _react.default.createElement("img", {
        alt: image.alt_text.altText,
        src: href
      });
    }
  }

  if (image.link && !opts.doNotLink) {
    ret = _react.default.createElement("a", {
      href: image.link,
      target: "_blank"
    }, ret);
  }

  return ret;
}

function renderImage(image) {
  return (0, _server.renderToStaticMarkup)(constructJSXImageElement(image));
}