'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-2e5758b6.js');
const uiStore = require('./ui-store-583d205a.js');
const index = require('./index-3f8e5441.js');
const string = require('./string-9bce3253.js');

const rtkPollCss = ":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}.scrollbar{scrollbar-width:thin;scrollbar-color:var(--rtk-scrollbar-color, rgb(var(--rtk-colors-background-600, 60 60 60)))\n    var(--rtk-scrollbar-background, transparent)}.scrollbar::-webkit-scrollbar{height:var(--rtk-space-1\\.5, 6px);width:var(--rtk-space-1\\.5, 6px);border-radius:9999px;background-color:var(--rtk-scrollbar-background, transparent)}.scrollbar::-webkit-scrollbar-thumb{border-radius:9999px;background-color:var(--rtk-scrollbar-color, rgb(var(--rtk-colors-background-600, 60 60 60)))}.ctr{margin-bottom:var(--rtk-space-3, 12px);display:flex;width:100%;flex-direction:column;color:rgb(var(--rtk-colors-text-1000, 255 255 255))}.poll-title{margin-top:var(--rtk-space-3, 12px);margin-bottom:var(--rtk-space-3, 12px);margin-right:var(--rtk-space-4, 16px);font-size:12px}.poll{border-radius:var(--rtk-border-radius-md, 8px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-800, 30 30 30) / var(--tw-bg-opacity));display:flex;flex-direction:column;padding:var(--rtk-space-4, 16px)}.poll-question{padding-left:var(--rtk-space-2, 8px);padding-right:var(--rtk-space-2, 8px);font-size:14px;overflow-wrap:break-word}.poll-answers{display:flex;flex-direction:row;justify-content:space-between;font-size:12px;margin-top:var(--rtk-space-4, 16px);margin-bottom:var(--rtk-space-1, 4px);padding-left:var(--rtk-space-1, 4px);padding-right:var(--rtk-space-3, 12px)}.poll-option{display:flex;flex-direction:column;margin-top:var(--rtk-space-2, 8px);border-radius:var(--rtk-border-radius-md, 8px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-700, 44 44 44) / var(--tw-bg-opacity));padding:var(--rtk-space-2, 8px);word-break:break-word}.poll-option.open-vote:hover{cursor:pointer}.poll-option label{display:flex;flex-direction:row;cursor:inherit}.poll-option-header{display:flex;flex-direction:row;justify-content:space-between;padding:var(--rtk-space-1, 4px)}.poll-option-header[data-disabled='true']:hover{cursor:default}.poll-option-header input{margin-top:var(--rtk-space-0, 0px);margin-bottom:var(--rtk-space-0, 0px);margin-left:var(--rtk-space-0\\.5, 2px);margin-right:var(--rtk-space-2, 8px);cursor:inherit}.poll-option-header .counter{color:rgb(var(--rtk-colors-text-1000, 255 255 255));font-size:12px}.votes{margin-top:var(--rtk-space-1, 4px);display:flex;flex-direction:row;flex-wrap:wrap}.vote{margin-right:var(--rtk-space-1, 4px);height:var(--rtk-space-8, 32px);width:var(--rtk-space-8, 32px);display:flex;align-items:center;justify-content:center;border-radius:var(--rtk-border-radius-md, 8px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-video-bg, 24 24 24) / var(--tw-bg-opacity));color:rgb(var(--rtk-colors-text-1000, 255 255 255));border-radius:var(--rtk-border-radius-none, 0);clip-path:circle()}.active{--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-brand-500, 33 96 253) / var(--tw-bg-opacity));color:rgb(var(--rtk-colors-text-on-brand-900, var(--rtk-colors-text-900, 255 255 255 / 0.88)))}.active .counter{color:rgb(var(--rtk-colors-text-on-brand-900, var(--rtk-colors-text-900, 255 255 255 / 0.88)))}";
const RtkPollStyle0 = rtkPollCss;

var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const RtkPolls = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.onVote = index$1.createEvent(this, "rtkVotePoll", 7);
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        this.MAX_VOTES_RENDER = 10;
    }
    vote(e, index) {
        if (this.poll.voted.includes(this.self)) {
            e.preventDefault();
        }
        else {
            this.onVote.emit({ id: this.poll.id, index });
        }
    }
    render() {
        const hasVoted = this.poll.voted.includes(this.self);
        return (index$1.h(index$1.Host, { key: '69e61c49e0f7f2015538447a8862bdb7fd3b4840' }, index$1.h("div", { key: '00dec58cb19f30a4fc01926dd383edb270cd61c3', class: "ctr" }, index$1.h("p", { key: '04d88ddf1f20e1a7e5b017f058b585723498f474', class: "poll-title" }, this.t('polls.by'), " ", string.shorten(string.formatName(this.poll.createdBy), 20)), index$1.h("div", { key: '5432167291df04bc4a4bfa23117df79c8512027d', class: "poll" }, index$1.h("p", { key: 'bee55a8193f96f582cf1c3006393740ab652dea7', class: "poll-question" }, this.poll.question), index$1.h("div", { key: 'aefe01131512e992e756196b5b451d4423bb9635', class: "poll-answers" }, index$1.h("span", { key: '973f566c9d84a6519f7ab307fd072215fe76f72d' }, this.t('polls.answers')), index$1.h("span", { key: '2a87b4bd72b99916cf20070c792742d04c1a44ca' }, this.poll.voted.length)), this.poll.options.map((item, index) => (index$1.h("div", { class: {
                active: item.votes.some((x) => x.id === this.self),
                'open-vote': this.permissions.polls.canVote && !hasVoted,
                'poll-option': true,
            } }, index$1.h("div", { class: "poll-option-header", "data-disabled": !this.permissions.polls.canVote }, index$1.h("label", null, !hasVoted && (index$1.h("input", { disabled: !this.permissions.polls.canVote, readOnly: hasVoted, type: "radio", checked: item.votes.some((x) => x.id === this.self), onClick: (e) => this.vote(e, index) })), index$1.h("p", null, item.text)), index$1.h("span", { class: "counter" }, item.count)), index$1.h("div", { class: "votes" }, item.votes.slice(0, this.MAX_VOTES_RENDER).map((vote) => {
            if (this.poll.anonymous && this.self !== this.poll.createdByUserId)
                return;
            return (index$1.h("rtk-tooltip", { label: vote.name }, index$1.h("div", { class: "vote" }, string.getInitials(vote.name))));
        }), item.votes.length > this.MAX_VOTES_RENDER && (index$1.h("rtk-tooltip", { label: `+${item.votes.length - this.MAX_VOTES_RENDER} more ` }, index$1.h("div", { class: "vote" }, "+", item.votes.length - this.MAX_VOTES_RENDER)))))))))));
    }
};
__decorate$1([
    index.SyncWithStore()
], RtkPolls.prototype, "iconPack", void 0);
__decorate$1([
    index.SyncWithStore()
], RtkPolls.prototype, "t", void 0);
RtkPolls.style = RtkPollStyle0;

const rtkPollFormCss = ":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}.scrollbar{scrollbar-width:thin;scrollbar-color:var(--rtk-scrollbar-color, rgb(var(--rtk-colors-background-600, 60 60 60)))\n    var(--rtk-scrollbar-background, transparent)}.scrollbar::-webkit-scrollbar{height:var(--rtk-space-1\\.5, 6px);width:var(--rtk-space-1\\.5, 6px);border-radius:9999px;background-color:var(--rtk-scrollbar-background, transparent)}.scrollbar::-webkit-scrollbar-thumb{border-radius:9999px;background-color:var(--rtk-scrollbar-color, rgb(var(--rtk-colors-background-600, 60 60 60)))}.create-poll{margin-top:var(--rtk-space-3, 12px);margin-bottom:var(--rtk-space-3, 12px);display:flex;flex:1 1 0%;flex-direction:column;padding:var(--rtk-space-3, 12px);border-radius:var(--rtk-border-radius-md, 8px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-700, 44 44 44) / var(--tw-bg-opacity))}.create-poll p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px);text-align:center}.create-poll textarea{display:flex;border-radius:var(--rtk-border-radius-sm, 4px);padding:var(--rtk-space-2, 8px);font-family:var(--rtk-font-family, sans-serif);border-width:var(--rtk-border-width-none, 0);border-style:none;font-weight:500;outline:2px solid transparent;outline-offset:2px;margin-top:var(--rtk-space-3, 12px);margin-bottom:var(--rtk-space-3, 12px);resize:vertical;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-900, 26 26 26) / var(--tw-bg-opacity));color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64))}.create-poll textarea:focus{outline-style:solid;outline-offset:2px;outline-color:rgb(var(--rtk-colors-background-600, 60 60 60))}.option{display:flex;flex-direction:row;align-items:center;margin-bottom:var(--rtk-space-3, 12px);width:100%}.option input{width:100%;border-radius:var(--rtk-border-radius-sm, 4px);padding:var(--rtk-space-2, 8px);border-width:var(--rtk-border-width-none, 0);border-style:none;outline:2px solid transparent;outline-offset:2px;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-900, 26 26 26) / var(--tw-bg-opacity));color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64))}.remove-option{margin-left:var(--rtk-space-2, 8px);width:var(--rtk-space-10, 40px);border-radius:var(--rtk-border-radius-sm, 4px)}.add-option{margin-bottom:var(--rtk-space-3, 12px)}label{margin-bottom:var(--rtk-space-3, 12px)}.error-text{margin-top:var(--rtk-space-3, 12px);text-align:center;font-size:12px;--tw-text-opacity:1;color:rgba(var(--rtk-colors-danger, 255 45 45) / var(--tw-text-opacity))}";
const RtkPollFormStyle0 = rtkPollFormCss;

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const RtkPoll = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.onCreate = index$1.createEvent(this, "rtkCreatePoll", 7);
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        /** Options */
        this.options = ['', ''];
        this.anonymous = false;
        this.hideVotes = true;
    }
    removeOption(index) {
        var _a;
        this.options = this.options.filter((_, ind) => ind !== index);
        if (((_a = this.error) === null || _a === void 0 ? void 0 : _a.code) === 1)
            this.error = undefined;
    }
    addOption() {
        this.options = [...this.options, ''];
    }
    updateOption(ev, index) {
        var _a;
        this.options[index] = ev.target.value;
        if (((_a = this.error) === null || _a === void 0 ? void 0 : _a.code) === 1)
            this.error = undefined;
    }
    handleSubmit() {
        const pollObject = {
            question: this.question.value,
            options: this.options,
            anonymous: this.anonymous,
            hideVotes: this.anonymous ? true : this.hideVotes,
        };
        if (!pollObject.question) {
            this.error = {
                code: 0,
                message: this.t('polls.errors.question_required'),
            };
            return;
        }
        if (this.options.filter((op) => op.trim() === '').length > 0) {
            this.error = {
                code: 1,
                message: this.t('polls.errors.empty_option'),
            };
            return;
        }
        this.onCreate.emit(pollObject);
    }
    render() {
        return (index$1.h(index$1.Host, { key: 'ff160dd70e52bfa8a0c5116acea73a16cb84b9b8' }, index$1.h("div", { key: '2f14946249e12cbc38db8cca86d370030286dc8e', class: "create-poll" }, index$1.h("p", { key: 'a6e3704c58e3526aa2e0f517f529c7ee48627f30' }, this.t('polls.question')), index$1.h("textarea", { key: '6b076d162f80cc4e08ecc9379e829f37d43d06bb', onInput: () => {
                if (this.error && this.error.code === 0)
                    this.error = undefined;
            }, ref: (e) => (this.question = e), placeholder: this.t('polls.question.placeholder') }), this.options.map((item, index) => (index$1.h("div", { class: "option" }, index$1.h("input", { placeholder: this.t('polls.option.placeholder'), value: item, onInput: (event) => this.updateOption(event, index) }), index > 1 && (index$1.h("rtk-button", { kind: "icon", class: "auto remove-option", variant: "secondary", onClick: () => this.removeOption(index) }, index$1.h("rtk-icon", { icon: this.iconPack.subtract })))))), index$1.h("rtk-button", { key: 'be4055b21c6c810d0721fe2daf3308011b2c6d8f', class: "add-option", variant: "secondary", onClick: () => this.addOption() }, this.t('polls.option')), index$1.h("label", { key: '0e22f9da27289adf831d5a9458237aa11758fd41' }, index$1.h("input", { key: '577da1d275259a4e9ebffedf153f52e799172c42', id: "anonymous", type: "checkbox", onChange: (e) => (this.anonymous = e.target.checked) }), this.t('polls.results.anon')), index$1.h("label", { key: 'c06bcbfb6d15125c614764002985d20713e672a4' }, index$1.h("input", { key: '9e9bb29126ed51c0bbd06481b44f690d737fbb64', id: "hideVotes", type: "checkbox", disabled: this.anonymous, checked: this.anonymous ? true : this.hideVotes, onChange: (e) => (this.hideVotes = e.target.checked) }), this.t('polls.results.hide')), index$1.h("rtk-button", { key: 'ab48323f46db1f6e485bea960f6d7ec6bb684075', kind: "wide", onClick: () => this.handleSubmit() }, this.t('polls.create')), this.error && index$1.h("span", { key: '1d61b167ee24001d0b66acdb361262bf7f2ffcc6', class: "error-text" }, this.error.message))));
    }
};
__decorate([
    index.SyncWithStore()
], RtkPoll.prototype, "iconPack", void 0);
__decorate([
    index.SyncWithStore()
], RtkPoll.prototype, "t", void 0);
RtkPoll.style = RtkPollFormStyle0;

exports.rtk_poll = RtkPolls;
exports.rtk_poll_form = RtkPoll;
