"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _omit2 = _interopRequireDefault(require("lodash/omit"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _immutable = require("immutable");
var _slateReact = require("slate-react");
var _slate = require("slate");
var _index = require("../index");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; } /* eslint-disable react/prop-types */
var _ref = process.env.NODE_ENV === "production" ? {
  name: "1xfnuhy-Shortcode",
  styles: "margin-top:0;margin-bottom:16px;&:first-of-type{margin-top:0;};label:Shortcode;"
} : {
  name: "1xfnuhy-Shortcode",
  styles: "margin-top:0;margin-bottom:16px;&:first-of-type{margin-top:0;};label:Shortcode;",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9NYXJrZG93bkNvbnRyb2wvY29tcG9uZW50cy9TaG9ydGNvZGUuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBbURrQiIsImZpbGUiOiIuLi8uLi8uLi8uLi9zcmMvTWFya2Rvd25Db250cm9sL2NvbXBvbmVudHMvU2hvcnRjb2RlLmpzIiwic291cmNlc0NvbnRlbnQiOlsiLyogZXNsaW50LWRpc2FibGUgcmVhY3QvcHJvcC10eXBlcyAqL1xuaW1wb3J0IFJlYWN0LCB7IHVzZVN0YXRlIH0gZnJvbSAncmVhY3QnO1xuaW1wb3J0IHsgY3NzIH0gZnJvbSAnQGVtb3Rpb24vcmVhY3QnO1xuaW1wb3J0IHsgZnJvbUpTIH0gZnJvbSAnaW1tdXRhYmxlJztcbmltcG9ydCB7IG9taXQgfSBmcm9tICdsb2Rhc2gnO1xuaW1wb3J0IHsgUmVhY3RFZGl0b3IsIHVzZVNsYXRlIH0gZnJvbSAnc2xhdGUtcmVh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NFZGl0b3JDb21wb25lbnQ9e3RydWV9XG4gICAgICAgICAgb25WYWxpZGF0ZU9iamVjdD17KCkgPT4ge319XG4gICAgICAgICAgaXNOZXdFZGl0b3JDb21wb25lbnQ9e2VsZW1lbnQuZGF0YS5zaG9ydGNvZGVOZXd9XG4gICAgICAgICAgaXNTZWxlY3RlZD17aXNTZWxlY3RlZH1cbiAgICAgICAgLz5cbiAgICAgICAge2NoaWxkcmVufVxuICAgICAgPC9kaXY+XG4gICAgKVxuICApO1xufVxuXG5leHBvcnQgZGVmYXVsdCBTaG9ydGNvZGU7XG4iXX0= */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
};
function Shortcode(props) {
  const editor = (0, _slateReact.useSlate)();
  const {
    element,
    dataKey = 'shortcodeData',
    children
  } = props;
  const EditorControl = (0, _index.getEditorControl)();
  const plugin = (0, _index.getEditorComponents)().get(element.data.shortcode);
  const fieldKeys = ['id', 'fromBlock', 'toBlock', 'toPreview', 'pattern', 'icon'];
  const field = (0, _immutable.fromJS)((0, _omit2.default)(plugin, fieldKeys));
  const [value, setValue] = (0, _react.useState)((0, _immutable.fromJS)(element.data[dataKey]));
  function handleChange(fieldName, value, metadata) {
    const path = _slateReact.ReactEditor.findPath(editor, element);
    const newProperties = {
      data: _objectSpread(_objectSpread({}, element.data), {}, {
        [dataKey]: value.toJS(),
        metadata
      })
    };
    _slate.Transforms.setNodes(editor, newProperties, {
      at: path
    });
    setValue(value);
  }
  function handleFocus() {
    const path = _slateReact.ReactEditor.findPath(editor, element);
    _slate.Transforms.select(editor, path);
  }
  const path = _slateReact.ReactEditor.findPath(editor, element);
  const isSelected = editor.selection && path && _slate.Range.isRange(editor.selection) && _slate.Range.includes(editor.selection, path);
  return !field.isEmpty() && (0, _react2.jsx)("div", {
    onClick: handleFocus,
    onFocus: handleFocus
  }, (0, _react2.jsx)(EditorControl, {
    css: _ref,
    value: value,
    field: field,
    onChange: handleChange,
    isEditorComponent: true,
    onValidateObject: () => {},
    isNewEditorComponent: element.data.shortcodeNew,
    isSelected: isSelected
  }), children);
}
var _default = exports.default = Shortcode;