import { ChangeDetectionStrategy, Component, Input, ViewChild, } from '@angular/core';
import { BasePollComponent } from '../base-poll.component';
import * as i0 from "@angular/core";
import * as i1 from "../../custom-templates.service";
import * as i2 from "../../chat-client.service";
import * as i3 from "../../channel.service";
import * as i4 from "../../notification.service";
import * as i5 from "../../message-actions.service";
import * as i6 from "@angular/common";
import * as i7 from "@ngx-translate/core";
import * as i8 from "../../modal/modal.component";
import * as i9 from "../../notification-list/notification-list.component";
import * as i10 from "../poll-options-list/poll-options-list.component";
import * as i11 from "./poll-results/poll-results-list/poll-results-list.component";
import * as i12 from "./poll-answers-list/poll-answers-list.component";
import * as i13 from "./upsert-answer/upsert-answer.component";
import * as i14 from "./add-option/add-option.component";
/**
 *
 */
export class PollActionsComponent extends BasePollComponent {
    constructor(customTemplatesService, chatClientService, cdRef, channelService, notificationService, messageService) {
        super(customTemplatesService, chatClientService, cdRef, channelService, notificationService);
        this.messageService = messageService;
        /**
         * If there are more options than this number, the "See all options" button will be displayed
         */
        this.maxOptionsDisplayed = 10;
        /**
         * The maximum number of options allowed for the poll, this is defined by Stream API
         */
        this.maxPollOptions = 100;
        this.name = '';
        this.options = [];
        this.isClosed = false;
        this.allowUserSuggestions = false;
        this.allowAnswers = false;
        this.answerCount = 0;
        this.isOwnPoll = false;
        this.selectedAction = undefined;
        this.isModalOpen = false;
        this.modalOpened = (action) => {
            this.selectedAction = action;
            if (!this.isModalOpen) {
                this.isModalOpen = true;
                this.messageService.modalOpenedForMessage.next(this.messageId);
            }
        };
        this.modalClosed = () => {
            this.selectedAction = undefined;
            if (this.isModalOpen) {
                this.isModalOpen = false;
                this.messageService.modalOpenedForMessage.next(undefined);
                this.markForCheck();
            }
        };
    }
    stateStoreSelector(poll, markForCheck) {
        const unsubscribe = poll.state.subscribeWithSelector((state) => ({
            options: state.options,
            is_closed: state.is_closed,
            allow_user_suggested_options: state.allow_user_suggested_options,
            allow_answers: state.allow_answers,
            answer_count: state.answers_count,
            created_by: state.created_by,
            name: state.name,
            own_answer: state.ownAnswer,
            max_votes_allowed: state.max_votes_allowed,
            own_votes_by_option_id: state.ownVotesByOptionId,
        }), (state) => {
            this.options = state.options;
            this.isClosed = state.is_closed ?? false;
            this.allowUserSuggestions = state.allow_user_suggested_options ?? false;
            this.allowAnswers = state.allow_answers ?? false;
            this.answerCount = state.answer_count ?? 0;
            this.isOwnPoll = state.created_by?.id === this.currentUser?.id;
            this.name = state.name;
            this.ownAnwer = state.own_answer ?? undefined;
            markForCheck();
        });
        return unsubscribe;
    }
    async closePoll() {
        try {
            await this.poll?.close();
            this.modalClosed();
            this.markForCheck();
        }
        catch (error) {
            this.notificationService.addTemporaryNotification('streamChat.Failed to end vote');
            throw error;
        }
    }
    getModalContext() {
        return {
            isOpen: this.isModalOpen,
            isOpenChangeHandler: (isOpen) => {
                if (isOpen) {
                    this.modalOpened(this.selectedAction);
                }
                else {
                    this.modalClosed();
                }
            },
            content: this[this.selectedAction],
        };
    }
}
PollActionsComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: PollActionsComponent, deps: [{ token: i1.CustomTemplatesService }, { token: i2.ChatClientService }, { token: i0.ChangeDetectorRef }, { token: i3.ChannelService }, { token: i4.NotificationService }, { token: i5.MessageActionsService }], target: i0.ɵɵFactoryTarget.Component });
PollActionsComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.0.4", type: PollActionsComponent, selector: "stream-poll-actions", inputs: { maxOptionsDisplayed: "maxOptionsDisplayed", maxPollOptions: "maxPollOptions" }, viewQueries: [{ propertyName: "allOptions", first: true, predicate: ["allOptions"], descendants: true }, { propertyName: "suggestOption", first: true, predicate: ["suggestOption"], descendants: true }, { propertyName: "addAnswer", first: true, predicate: ["addAnswer"], descendants: true }, { propertyName: "viewComments", first: true, predicate: ["viewComments"], descendants: true }, { propertyName: "viewResults", first: true, predicate: ["viewResults"], descendants: true }, { propertyName: "endVote", first: true, predicate: ["endVote"], descendants: true }], usesInheritance: true, ngImport: i0, template: "<div class=\"str-chat__poll-actions\">\n  <ng-container\n    *ngIf=\"maxOptionsDisplayed && options.length > maxOptionsDisplayed\"\n  >\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('allOptions')\">\n      {{ \"streamChat.See all options ({{count}})\" | translate:{count:\n      options.length} }}\n    </button>\n  </ng-container>\n  <ng-container\n    *ngIf=\"\n      allowUserSuggestions &&\n      canVote &&\n      !isClosed &&\n      options.length < maxPollOptions\n    \"\n  >\n    <button\n      class=\"str-chat__poll-action\"\n      (click)=\"modalOpened('suggestOption')\"\n    >\n      {{ \"streamChat.Suggest an option\" | translate }}\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"allowAnswers && canVote && !isClosed\">\n    <button\n      class=\"str-chat__poll-action\"\n      (click)=\"modalOpened('addAnswer')\"\n      (keyup.enter)=\"modalOpened('addAnswer')\"\n    >\n      <ng-container *ngIf=\"ownAnwer; else newAnswer\">\n        {{ \"streamChat.Update your comment\" | translate }}\n      </ng-container>\n      <ng-template #newAnswer>\n        {{ \"streamChat.Add a comment\" | translate }}\n      </ng-template>\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"answerCount > 0 && canQueryVotes\">\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('viewComments')\">\n      {{ (answerCount === 1 ? \"streamChat.View {{ count }} comment\" :\n      \"streamChat.View {{ count }} comments\") | translate:{count: answerCount}\n      }}\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"canQueryVotes\">\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('viewResults')\">\n      {{ \"streamChat.View results\" | translate }}\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"isOwnPoll && !isClosed\">\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('endVote')\">\n      {{ \"streamChat.End vote\" | translate }}\n    </button>\n  </ng-container>\n</div>\n\n<ng-container *ngIf=\"selectedAction\">\n  <ng-container\n    *ngTemplateOutlet=\"\n      (customTemplatesService.modalTemplate$ | async) || defaultModal;\n      context: getModalContext()\n    \"\n  ></ng-container>\n</ng-container>\n\n<ng-template\n  #defaultModal\n  let-isOpen=\"isOpen\"\n  let-isOpenChangeHandler=\"isOpenChangeHandler\"\n  let-content=\"content\"\n>\n  <stream-modal\n    class=\"str-chat__poll-actions str-chat-angular__poll-actions\"\n    [isOpen]=\"isOpen\"\n    [content]=\"content\"\n    (isOpenChange)=\"isOpenChangeHandler($event)\"\n  >\n  </stream-modal>\n</ng-template>\n\n<ng-template #allOptions>\n  <div class=\"str-chat__modal__poll-option-list str-chat-angular__poll-actions\">\n    <div class=\"str-chat__modal-header\">\n      <div class=\"str-chat__modal-header__title\" translate>\n        streamChat.Poll options\n      </div>\n    </div>\n    <div class=\"str-chat__modal__poll-option-list__body\">\n      <div class=\"str-chat__modal__poll-option-list__title\">{{ name }}</div>\n      <stream-poll-options-list\n        [pollId]=\"pollId\"\n        [messageId]=\"messageId\"\n        [maxOptionsDisplayed]=\"undefined\"\n      ></stream-poll-options-list>\n      <stream-notification-list></stream-notification-list>\n    </div>\n  </div>\n</ng-template>\n<ng-template #suggestOption>\n  <stream-add-option\n    [pollId]=\"pollId\"\n    [messageId]=\"messageId\"\n    [closeModal]=\"modalClosed\"\n  ></stream-add-option>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #addAnswer>\n  <stream-upsert-answer\n    [pollId]=\"pollId\"\n    [messageId]=\"messageId\"\n    [answer]=\"ownAnwer\"\n    [closeModal]=\"modalClosed\"\n  ></stream-upsert-answer>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #viewComments>\n  <stream-poll-answers-list\n    (upsertOwnAnswer)=\"modalOpened('addAnswer')\"\n    [pollId]=\"pollId\"\n    [messageId]=\"messageId\"\n  ></stream-poll-answers-list>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #viewResults>\n  <stream-poll-results-list\n    [messageId]=\"messageId\"\n    [pollId]=\"pollId\"\n  ></stream-poll-results-list>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #endVote>\n  <div\n    class=\"str-chat__dialog str-chat__dialog--prompt str-chat__modal__end-vote\"\n  >\n    <div class=\"str-chat__dialog__body\">\n      <div class=\"str-chat__dialog__title\" translate>streamChat.End vote</div>\n      <div class=\"str-chat__dialog__prompt\" translate>\n        streamChat.After a poll is closed, no more votes can be cast\n      </div>\n      <stream-notification-list></stream-notification-list>\n      <div class=\"str-chat__dialog__controls\">\n        <button\n          class=\"str-chat__dialog__controls-button\"\n          (click)=\"modalClosed()\"\n        >\n          {{ \"streamChat.Cancel\" | translate }}\n        </button>\n        <button class=\"str-chat__dialog__controls-button\" (click)=\"closePoll()\">\n          {{ \"streamChat.End vote\" | translate }}\n        </button>\n      </div>\n    </div>\n  </div>\n</ng-template>\n", dependencies: [{ kind: "directive", type: i6.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i6.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i7.TranslateDirective, selector: "[translate],[ngx-translate]", inputs: ["translate", "translateParams"] }, { kind: "component", type: i8.ModalComponent, selector: "stream-modal", inputs: ["isOpen", "content"], outputs: ["isOpenChange"] }, { kind: "component", type: i9.NotificationListComponent, selector: "stream-notification-list" }, { kind: "component", type: i10.PollOptionsListComponent, selector: "stream-poll-options-list", inputs: ["maxOptionsDisplayed"] }, { kind: "component", type: i11.PollResultsListComponent, selector: "stream-poll-results-list" }, { kind: "component", type: i12.PollAnswersListComponent, selector: "stream-poll-answers-list", outputs: ["upsertOwnAnswer"] }, { kind: "component", type: i13.UpsertAnswerComponent, selector: "stream-upsert-answer", inputs: ["answer", "closeModal"] }, { kind: "component", type: i14.AddOptionComponent, selector: "stream-add-option", inputs: ["closeModal"] }, { kind: "pipe", type: i6.AsyncPipe, name: "async" }, { kind: "pipe", type: i7.TranslatePipe, name: "translate" }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: PollActionsComponent, decorators: [{
            type: Component,
            args: [{ selector: 'stream-poll-actions', changeDetection: ChangeDetectionStrategy.OnPush, template: "<div class=\"str-chat__poll-actions\">\n  <ng-container\n    *ngIf=\"maxOptionsDisplayed && options.length > maxOptionsDisplayed\"\n  >\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('allOptions')\">\n      {{ \"streamChat.See all options ({{count}})\" | translate:{count:\n      options.length} }}\n    </button>\n  </ng-container>\n  <ng-container\n    *ngIf=\"\n      allowUserSuggestions &&\n      canVote &&\n      !isClosed &&\n      options.length < maxPollOptions\n    \"\n  >\n    <button\n      class=\"str-chat__poll-action\"\n      (click)=\"modalOpened('suggestOption')\"\n    >\n      {{ \"streamChat.Suggest an option\" | translate }}\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"allowAnswers && canVote && !isClosed\">\n    <button\n      class=\"str-chat__poll-action\"\n      (click)=\"modalOpened('addAnswer')\"\n      (keyup.enter)=\"modalOpened('addAnswer')\"\n    >\n      <ng-container *ngIf=\"ownAnwer; else newAnswer\">\n        {{ \"streamChat.Update your comment\" | translate }}\n      </ng-container>\n      <ng-template #newAnswer>\n        {{ \"streamChat.Add a comment\" | translate }}\n      </ng-template>\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"answerCount > 0 && canQueryVotes\">\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('viewComments')\">\n      {{ (answerCount === 1 ? \"streamChat.View {{ count }} comment\" :\n      \"streamChat.View {{ count }} comments\") | translate:{count: answerCount}\n      }}\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"canQueryVotes\">\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('viewResults')\">\n      {{ \"streamChat.View results\" | translate }}\n    </button>\n  </ng-container>\n  <ng-container *ngIf=\"isOwnPoll && !isClosed\">\n    <button class=\"str-chat__poll-action\" (click)=\"modalOpened('endVote')\">\n      {{ \"streamChat.End vote\" | translate }}\n    </button>\n  </ng-container>\n</div>\n\n<ng-container *ngIf=\"selectedAction\">\n  <ng-container\n    *ngTemplateOutlet=\"\n      (customTemplatesService.modalTemplate$ | async) || defaultModal;\n      context: getModalContext()\n    \"\n  ></ng-container>\n</ng-container>\n\n<ng-template\n  #defaultModal\n  let-isOpen=\"isOpen\"\n  let-isOpenChangeHandler=\"isOpenChangeHandler\"\n  let-content=\"content\"\n>\n  <stream-modal\n    class=\"str-chat__poll-actions str-chat-angular__poll-actions\"\n    [isOpen]=\"isOpen\"\n    [content]=\"content\"\n    (isOpenChange)=\"isOpenChangeHandler($event)\"\n  >\n  </stream-modal>\n</ng-template>\n\n<ng-template #allOptions>\n  <div class=\"str-chat__modal__poll-option-list str-chat-angular__poll-actions\">\n    <div class=\"str-chat__modal-header\">\n      <div class=\"str-chat__modal-header__title\" translate>\n        streamChat.Poll options\n      </div>\n    </div>\n    <div class=\"str-chat__modal__poll-option-list__body\">\n      <div class=\"str-chat__modal__poll-option-list__title\">{{ name }}</div>\n      <stream-poll-options-list\n        [pollId]=\"pollId\"\n        [messageId]=\"messageId\"\n        [maxOptionsDisplayed]=\"undefined\"\n      ></stream-poll-options-list>\n      <stream-notification-list></stream-notification-list>\n    </div>\n  </div>\n</ng-template>\n<ng-template #suggestOption>\n  <stream-add-option\n    [pollId]=\"pollId\"\n    [messageId]=\"messageId\"\n    [closeModal]=\"modalClosed\"\n  ></stream-add-option>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #addAnswer>\n  <stream-upsert-answer\n    [pollId]=\"pollId\"\n    [messageId]=\"messageId\"\n    [answer]=\"ownAnwer\"\n    [closeModal]=\"modalClosed\"\n  ></stream-upsert-answer>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #viewComments>\n  <stream-poll-answers-list\n    (upsertOwnAnswer)=\"modalOpened('addAnswer')\"\n    [pollId]=\"pollId\"\n    [messageId]=\"messageId\"\n  ></stream-poll-answers-list>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #viewResults>\n  <stream-poll-results-list\n    [messageId]=\"messageId\"\n    [pollId]=\"pollId\"\n  ></stream-poll-results-list>\n  <stream-notification-list></stream-notification-list>\n</ng-template>\n<ng-template #endVote>\n  <div\n    class=\"str-chat__dialog str-chat__dialog--prompt str-chat__modal__end-vote\"\n  >\n    <div class=\"str-chat__dialog__body\">\n      <div class=\"str-chat__dialog__title\" translate>streamChat.End vote</div>\n      <div class=\"str-chat__dialog__prompt\" translate>\n        streamChat.After a poll is closed, no more votes can be cast\n      </div>\n      <stream-notification-list></stream-notification-list>\n      <div class=\"str-chat__dialog__controls\">\n        <button\n          class=\"str-chat__dialog__controls-button\"\n          (click)=\"modalClosed()\"\n        >\n          {{ \"streamChat.Cancel\" | translate }}\n        </button>\n        <button class=\"str-chat__dialog__controls-button\" (click)=\"closePoll()\">\n          {{ \"streamChat.End vote\" | translate }}\n        </button>\n      </div>\n    </div>\n  </div>\n</ng-template>\n" }]
        }], ctorParameters: function () { return [{ type: i1.CustomTemplatesService }, { type: i2.ChatClientService }, { type: i0.ChangeDetectorRef }, { type: i3.ChannelService }, { type: i4.NotificationService }, { type: i5.MessageActionsService }]; }, propDecorators: { maxOptionsDisplayed: [{
                type: Input
            }], maxPollOptions: [{
                type: Input
            }], allOptions: [{
                type: ViewChild,
                args: ['allOptions']
            }], suggestOption: [{
                type: ViewChild,
                args: ['suggestOption']
            }], addAnswer: [{
                type: ViewChild,
                args: ['addAnswer']
            }], viewComments: [{
                type: ViewChild,
                args: ['viewComments']
            }], viewResults: [{
                type: ViewChild,
                args: ['viewResults']
            }], endVote: [{
                type: ViewChild,
                args: ['endVote']
            }] } });
//# sourceMappingURL=data:application/json;base64, 