import React from "react";
import { PollData_Layout_PollLayout_Type } from "./../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { RCEHelpersPropTypes } from "../rce-helpers-context/index.js";
import { OptionPropTypes } from "./types.js";
class PollOptionBase extends React.PureComponent {
  constructor() {
    super(...arguments);
    this.state = {
      loading: false,
      checked: this.isUserChoice()
    };
    this.handleRemove = (e) => {
      e.stopPropagation();
      this.props.remove();
    };
    this.handleVoteClick = async (e) => {
      const { validateUser, onBeforeVote, preventInteraction, preventVoting } = this.props.rce;
      const vote = this.props.poll.settings.permissions?.vote;
      const onVoteClick = onBeforeVote || validateUser;
      e.preventDefault();
      if (e.key || preventInteraction || preventVoting) {
        return;
      }
      if (onVoteClick) {
        onVoteClick({ voteRole: vote }).then(this.toggleVote.bind(this), () => {
        });
      } else {
        this.toggleVote();
      }
    };
    this.handleTitleChange = (title) => {
      const { update, option } = this.props;
      return update({ ...option, title });
    };
    this.handleImageChange = (url) => {
      const { update, option } = this.props;
      const newOption = { ...option, image: { src: { url } } };
      return update(newOption);
    };
  }
  isEditAllowed() {
    return !this.props.option.count && !this.props.rce.isPreview;
  }
  isUserChoice() {
    const { poll, option } = this.props;
    return !!poll.ownVotes?.includes(option.id);
  }
  isViewMode() {
    return this.props.rce.isViewMode;
  }
  isGridLayout() {
    return this.props.layout.poll?.type === PollData_Layout_PollLayout_Type.GRID;
  }
  isListLayout() {
    return this.props.layout.poll?.type === PollData_Layout_PollLayout_Type.LIST;
  }
  isImageEnabled() {
    return this.props.layout.options?.enableImage;
  }
  async toggleVote() {
    this.setState({ loading: true });
    try {
      if (this.isUserChoice()) {
        await this.props.unvote(this.props.option.id);
        this.setState({ checked: false });
      } else {
        await this.props.vote(this.props.option.id);
        this.setState({ checked: true });
      }
    } catch {
    } finally {
      this.setState({ loading: false });
    }
  }
  getVotePercentage() {
    const { option } = this.props;
    return option.rating || 0;
  }
  render() {
    if (this.isViewMode()) {
      return this.renderViewMode();
    }
    return this.renderEditMode();
  }
}
PollOptionBase.propTypes = {
  ...OptionPropTypes,
  ...RCEHelpersPropTypes
};
export {
  PollOptionBase
};
//# sourceMappingURL=poll-option-base.js.map
