import lodash from "lodash";
import PropTypes from "prop-types";
import React from "react";
const EditorEvents = { PUBLISH: "plugin:publish", RICOS_PUBLISH: "ricos:publish" };
const EditorEventsContext = React.createContext({
  subscribe() {
    return () => {
    };
  },
  unsubscribe() {
  },
  dispatch() {
    return Promise.resolve(true);
  },
  publish() {
    return Promise.resolve({});
  }
});
const withEditorEvents = (WrappedComponent) => (props) => /* @__PURE__ */ React.createElement(EditorEventsContext.Consumer, null, (contextValue) => /* @__PURE__ */ React.createElement(WrappedComponent, { editorEvents: contextValue, ...props }));
class EditorEventsProvider extends React.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) {
    lodash.remove(this.events[event], (callback) => callback === cb);
  }
  render() {
    const { children } = this.props;
    return /* @__PURE__ */ React.createElement(EditorEventsContext.Provider, { value: this.state }, children);
  }
}
EditorEventsProvider.propTypes = {
  children: PropTypes.node
};
const withEditorContext = (WrappedComponent) => {
  const WrappedComponentWithEvents = withEditorEvents(WrappedComponent);
  const withEditorProivder = (props) => {
    return /* @__PURE__ */ React.createElement(EditorEventsProvider, null, /* @__PURE__ */ React.createElement(WrappedComponentWithEvents, { ...props }));
  };
  return withEditorProivder;
};
export {
  EditorEvents,
  EditorEventsContext,
  EditorEventsProvider,
  withEditorContext,
  withEditorEvents
};
//# sourceMappingURL=editor-events-context.js.map
