/*
 * Draftail plugin for the "button-link" feature (see dnscms/rich_text.py).
 * Reuses Wagtail's link chooser (source) and edit/remove tooltip (decorator),
 * so buttons insert and edit like normal links. No build step: uses Wagtail's
 * globals and React.createElement.
 */
(function () {
  "use strict";

  var draftail = window.draftail;
  var React = window.React;
  var Icon = window.wagtail.components.Icon;
  var TooltipEntity = draftail.TooltipEntity;

  // Wrap TooltipEntity so CSS can style it as a button
  function ButtonLink(props) {
    var url = props.contentState.getEntity(props.entityKey).getData().url || null;
    return React.createElement(
      "span",
      { className: "button-link-entity" },
      React.createElement(
        TooltipEntity,
        Object.assign({}, props, {
          icon: React.createElement(Icon, { name: "arrow-right-full" }),
          label: url || "",
          url: url,
        })
      )
    );
  }

  draftail.registerPlugin(
    {
      type: "BUTTON",
      source: draftail.LinkModalWorkflowSource,
      decorator: ButtonLink,
    },
    "entityTypes"
  );
})();
