import lodash from "lodash";
import PropTypes from "prop-types";
import React, { PureComponent } from "react";
import { OPTION_IMAGES_POOL } from "../../defaults.js";
import { getRandomValue } from "../../helpers.js";
import { PollPropTypes } from "./types.js";
const PollContext = React.createContext({});
const withPoll = (WrappedComponent) => (props) => {
  return /* @__PURE__ */ React.createElement(PollContext.Consumer, null, (contextValue) => {
    return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, ...contextValue });
  });
};
class PollContextProvider extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      changePollTitle: this.changePollTitle.bind(this),
      updatePollOption: this.updatePollOption.bind(this),
      addOption: this.addOption.bind(this),
      removeOption: this.removeOption.bind(this),
      changePollImage: this.changePollImage.bind(this),
      fetchPoll: this.fetchPoll.bind(this),
      vote: this.vote.bind(this),
      unvote: this.unvote.bind(this),
      getVoters: this.getVoters.bind(this),
      // @ts-expect-error
      poll: this.props.poll
    };
    this.syncPoll = async () => {
      let dto;
      const poll = this.populateWithData(this.state.poll);
      if (poll.id) {
        dto = await this.pollApiClient.updatePoll(poll);
      } else {
        dto = await this.pollApiClient.createPoll(poll);
      }
      this.props.setPoll(dto);
      console.debug("polls publish callback");
    };
    this.pollApiClient = props.settings?.pollServiceApi;
  }
  componentWillReceiveProps(props) {
    if (!props.settings?.pollServiceApi && // @ts-expect-error
    props.settings.siteToken !== this.props.settings.siteToken) {
      this.fetchPoll();
    }
    if (JSON.stringify(props.poll) !== JSON.stringify(this.props.poll)) {
      this.setState(() => ({ poll: props.poll }));
    }
  }
  componentDidMount() {
    const { editorEvents } = this.props;
    editorEvents?.subscribe("plugin:publish", this.syncPoll);
  }
  componentWillUnmount() {
    this.props.editorEvents?.unsubscribe("plugin:publish", this.syncPoll);
  }
  async fetchPoll() {
    if (!this.state.poll.id) {
      return;
    }
    const poll = await this.pollApiClient.fetchPoll(this.state.poll.id);
    this.props.setPoll?.(poll);
    this.setState(() => ({ poll }));
  }
  populateWithData(poll) {
    const { t } = this.props;
    poll.title = poll.title || t("Poll_Editor_Question_Placeholder");
    poll.options = poll.options.map((option) => ({
      ...option,
      title: option.title || t("Poll_Editor_Answer_Placeholder")
    }));
    return poll;
  }
  updatePoll(poll) {
    this.props.setPoll(Object.assign({}, this.state.poll, poll));
  }
  async vote(optionId) {
    const poll = await this.pollApiClient.vote(this.state.poll.id, optionId);
    this.setState({ poll });
  }
  async unvote(optionId) {
    const poll = await this.pollApiClient.unvote(this.state.poll.id, optionId);
    this.setState({ poll });
  }
  getVoters(optionId, params) {
    return this.pollApiClient.getVoters(this.state.poll.id, optionId, params);
  }
  changePollTitle(title) {
    return this.updatePoll({ title });
  }
  changePollImage(url) {
    const imageData = { image: { src: { url } } };
    return this.updatePoll(imageData);
  }
  updatePollOption(index, option) {
    const poll = lodash.cloneDeep(this.state.poll);
    poll.options[index] = option;
    return this.updatePoll({ options: poll.options });
  }
  addOption() {
    const poll = lodash.cloneDeep(this.state.poll);
    const imageData = {
      image: {
        src: {
          url: getRandomValue(OPTION_IMAGES_POOL)
        }
      }
    };
    poll.options.push({
      title: "",
      ...imageData
    });
    return this.updatePoll({ options: poll.options });
  }
  removeOption(index) {
    const poll = lodash.cloneDeep(this.state.poll);
    poll.options.splice(index, 1);
    return this.updatePoll({ options: poll.options });
  }
  render() {
    const { children } = this.props;
    return /* @__PURE__ */ React.createElement(PollContext.Provider, { value: this.state }, children);
  }
}
PollContextProvider.propTypes = {
  editorEvents: PropTypes.shape({
    subscribe: PropTypes.func,
    unsubscribe: PropTypes.func
  }),
  settings: PropTypes.shape({
    siteToken: PropTypes.string,
    isWebView: PropTypes.bool,
    getSiteMembers: PropTypes.func,
    pollServiceApi: PropTypes.object
  }),
  poll: PropTypes.shape(PollPropTypes),
  setPoll: PropTypes.func,
  children: PropTypes.any,
  t: PropTypes.func
};
export {
  PollContextProvider,
  withPoll
};
//# sourceMappingURL=poll-context.js.map
