"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 poll_option_base_exports = {};
__export(poll_option_base_exports, {
  PollOptionBase: () => PollOptionBase
});
module.exports = __toCommonJS(poll_option_base_exports);
var import_react = __toESM(require("react"));
var import_v1 = require("./../../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_rce_helpers_context = require("../rce-helpers-context/index.js");
var import_types = require("./types.js");
class PollOptionBase extends import_react.default.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 === import_v1.PollData_Layout_PollLayout_Type.GRID;
  }
  isListLayout() {
    return this.props.layout.poll?.type === import_v1.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 = {
  ...import_types.OptionPropTypes,
  ...import_rce_helpers_context.RCEHelpersPropTypes
};
//# sourceMappingURL=poll-option-base.js.map
