"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var editor_events_context_exports = {};
__export(editor_events_context_exports, {
  EditorEvents: () => EditorEvents,
  EditorEventsContext: () => EditorEventsContext,
  EditorEventsProvider: () => EditorEventsProvider,
  withEditorContext: () => withEditorContext,
  withEditorEvents: () => withEditorEvents
});
module.exports = __toCommonJS(editor_events_context_exports);
var import_lodash = __toESM(require("lodash"));
var import_prop_types = __toESM(require("prop-types"));
var import_react = __toESM(require("react"));
const EditorEvents = { PUBLISH: "plugin:publish", RICOS_PUBLISH: "ricos:publish" };
const EditorEventsContext = import_react.default.createContext({
  subscribe() {
    return () => {
    };
  },
  unsubscribe() {
  },
  dispatch() {
    return Promise.resolve(true);
  },
  publish() {
    return Promise.resolve({});
  }
});
const withEditorEvents = (WrappedComponent) => (props) => /* @__PURE__ */ import_react.default.createElement(EditorEventsContext.Consumer, null, (contextValue) => /* @__PURE__ */ import_react.default.createElement(WrappedComponent, { editorEvents: contextValue, ...props }));
class EditorEventsProvider extends import_react.default.Component {
  constructor() {
    super(...arguments);
    this.state = {
      subscribe: this.subscribe.bind(this),
      unsubscribe: this.unsubscribe.bind(this),
      dispatch: this.dispatch.bind(this),
      publish: this.publish.bind(this)
    };
    this.events = {};
  }
  dispatch(event, data) {
    const callbacks = this.events[event] || [];
    return Promise.all(callbacks.map((cb) => cb(data)));
  }
  publish() {
    return this.dispatch(EditorEvents.PUBLISH, void 0).then(() => {
      return this.dispatch(EditorEvents.RICOS_PUBLISH, void 0).then(async (publishResponse) => {
        const editorResponse = publishResponse.filter(
          ({ type } = {}) => type === "EDITOR_PUBLISH"
        )[0];
        return editorResponse?.data;
      });
    });
  }
  subscribe(event, cb) {
    this.events[event] = this.events[event] || [];
    this.events[event].push(cb);
    return () => this.unsubscribe(event, cb);
  }
  unsubscribe(event, cb) {
    import_lodash.default.remove(this.events[event], (callback) => callback === cb);
  }
  render() {
    const { children } = this.props;
    return /* @__PURE__ */ import_react.default.createElement(EditorEventsContext.Provider, { value: this.state }, children);
  }
}
EditorEventsProvider.propTypes = {
  children: import_prop_types.default.node
};
const withEditorContext = (WrappedComponent) => {
  const WrappedComponentWithEvents = withEditorEvents(WrappedComponent);
  const withEditorProivder = (props) => {
    return /* @__PURE__ */ import_react.default.createElement(EditorEventsProvider, null, /* @__PURE__ */ import_react.default.createElement(WrappedComponentWithEvents, { ...props }));
  };
  return withEditorProivder;
};
//# sourceMappingURL=editor-events-context.js.map
