"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ICON_TYPE = exports.CONTENT_TYPE = void 0;
exports["default"] = CustomPlugin;

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

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

var _serialization = require("../../serialization");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

// We're possibly going to have to support content types, so starting it as an enum
var CONTENT_TYPE = {
  FRAGMENT: 'FRAGMENT'
}; // We're possibly going to have to support multiple icon types, so starting it as an enum

exports.CONTENT_TYPE = CONTENT_TYPE;
var ICON_TYPE = {
  SVG: 'SVG'
};
exports.ICON_TYPE = ICON_TYPE;

var getIcon = function getIcon(customPluginProps) {
  var svg = customPluginProps.icon;

  switch (customPluginProps.iconType) {
    case ICON_TYPE.SVG:
      return /*#__PURE__*/_react["default"].createElement("span", {
        style: {
          width: 28,
          height: 28
        },
        dangerouslySetInnerHTML: {
          __html: svg
        }
      });

    default:
      return /*#__PURE__*/_react["default"].createElement("span", null, customPluginProps.iconAlt);
  }
};

function CustomPlugin(type, customPluginProps) {
  var toolbar = {
    icon: getIcon(customPluginProps),
    onClick: function onClick(value, onChange, getFocusedValue) {
      var editorDOM = document.querySelector("[data-key=\"".concat(value.document.key, "\"]"));
      var valueToUse = value;

      var callback = function callback(_ref, focus) {
        var customContent = _ref.customContent,
            contentType = _ref.contentType;
        valueToUse = getFocusedValue();

        switch (contentType) {
          case CONTENT_TYPE.FRAGMENT:
          default:
            {
              var contentValue = (0, _serialization.htmlToValue)(customContent);
              var change = valueToUse.change().insertFragment(contentValue.document);
              valueToUse = change.value;
              onChange(change);
              break;
            }
        }

        if (focus) {
          if (editorDOM) {
            editorDOM.focus();
          }
        }
      }; // NOTE: the emitted event (custom event named by client) will be suffixed with "PIE-"


      window.dispatchEvent(new CustomEvent("PIE-".concat(customPluginProps.event), {
        detail: _objectSpread(_objectSpread({}, customPluginProps), {}, {
          callback: callback
        })
      }));
    },
    supports: function supports(node) {
      return node.object === 'inline' && node.type === type;
    }
  };
  return {
    name: type,
    toolbar: toolbar,
    renderNode: function renderNode(props) {
      if (props.node.type === type) {
        var node = props.node;
        var data = node.data;
        var jsonData = data.toJSON();
        var customContent = jsonData.customContent,
            contentType = jsonData.contentType;

        switch (contentType) {
          case CONTENT_TYPE.FRAGMENT:
          default:
            return customContent;
        }
      }
    }
  };
}
//# sourceMappingURL=index.js.map