import { Component, Input } from '@angular/core';
import { equals } from '@aztrix/helpers';
import { AgreementConfirmation, AgreementStatus, AgreementVerification, } from '@aztrix/models';
import { ProposalsService } from '@aztrix/sdk';
import { distinctUntilChanged, map, ReplaySubject, shareReplay, switchMap } from 'rxjs';
import { AbstractSelectSearch } from '../abstract-select-search';
import * as i0 from "@angular/core";
import * as i1 from "@aztrix/sdk";
import * as i2 from "@angular/common";
import * as i3 from "../select-search/select-search.component";
import * as i4 from "@aztrix/components/row-cell-view";
import * as i5 from "@aztrix/helpers";
import * as i6 from "./proposal-by-id.pipe";
export class ProposalAgreementsSelectSearchComponent extends AbstractSelectSearch {
    constructor(_proposalsService) {
        super();
        this._proposalsService = _proposalsService;
        this._proposals$ = new ReplaySubject(1);
        this.searchCallback$ = (_options, query) => {
            return this._proposals$.pipe(distinctUntilChanged((p1, p2) => equals(p1, p2)), switchMap((proposals) => {
                const searchOptions = {
                    agreementStatus: AgreementStatus.AGREEMENTSTATUS_UNDEFINED,
                    agreementConfirmation: AgreementConfirmation.AGREEMENTCONFIRMATION_UNDEFINED,
                    agreementVerification: AgreementVerification.AGREEMENTVERIFICATION_UNDEFINED,
                    limit: 20,
                    query: query,
                };
                let observable = this._proposalsService.searchAgreements(searchOptions);
                if (proposals.length === 1) {
                    observable = this._proposalsService.searchAgreementsOfProposal(proposals[0].id, searchOptions);
                }
                return observable.pipe(map((result) => result.results));
            }), shareReplay(1));
        };
    }
    ngOnChanges(changes) {
        if (changes.proposals && this.proposals) {
            this._proposals$.next(this.proposals);
        }
    }
}
ProposalAgreementsSelectSearchComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ProposalAgreementsSelectSearchComponent, deps: [{ token: i1.ProposalsService }], target: i0.ɵɵFactoryTarget.Component });
ProposalAgreementsSelectSearchComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.1.5", type: ProposalAgreementsSelectSearchComponent, selector: "ax-proposal-agreements-select-search", inputs: { proposals: "proposals" }, usesInheritance: true, usesOnChanges: true, ngImport: i0, template: "<ng-template #option let-result>\n  <ax-agreement-cell-view\n    *ngIf=\"result\"\n    [agreement]=\"result\"\n    [proposal]=\"proposals | proposalById: result.proposalId\"\n  >\n    <div class=\"status\" *ngIf=\"result.agreementData?.confirmed\">\n      <span>\n        {{ result.active ? ('agreement.active' | translate) : ('agreement.expired' | translate) }}\n      </span>\n      <div class=\"status-indicator\" [ngClass]=\"{'is-expired': result.expirationTimestamp}\"></div>\n    </div>\n    <div class=\"status\" *ngIf=\"!result.agreementData?.confirmed\">\n      <span>{{ 'agreement.status.not-confirmed' | translate }}</span>\n      <div class=\"status-indicator is-pending\"></div>\n    </div>\n  </ax-agreement-cell-view>\n</ng-template>\n\n<ng-template #selectedViewTemplate let-result>\n  <ax-agreement-cell-view\n    class=\"selected\"\n    *ngIf=\"result\"\n    [agreement]=\"result\"\n    [noAvatar]=\"true\"\n    [proposal]=\"proposals | proposalById: result.proposalId\"\n  ></ax-agreement-cell-view>\n</ng-template>\n\n<ax-select-search\n  [form]=\"form\"\n  [placeholder]=\"(multiple ? 'label.agreements' : 'label.agreement') | translate\"\n  [optionTemplate]=\"option\"\n  [selectedValueTemplate]=\"selectedViewTemplate\"\n  [multiple]=\"multiple\"\n  [searchCallback$]=\"searchCallback$\"\n  [valueCallback]=\"valueCallback\"\n  [label]=\"label ? label : ((multiple ? 'label.agreements' : 'label.agreement') | translate)\"\n></ax-select-search>\n", styles: [".status{display:flex;align-items:center}.status-indicator{background-color:var(--primary-color-500);width:8px;height:8px;border-radius:6px;margin:0 6px}.status-indicator.is-pending{background-color:#e67e22}.status-indicator.is-expired{background-color:var(--warn-color-500)}.selected{height:38px}\n"], dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i3.SelectSearchComponent, selector: "ax-select-search", inputs: ["form", "options", "placeholder", "optionTemplate", "selectedValueTemplate", "multiple", "label", "keys", "canAddNew", "required", "appearance", "errorsTemplate", "prefixTemplate", "disabled", "valueCallback", "searchCallback$"], outputs: ["addNewValue"] }, { kind: "component", type: i4.AgreementCellViewComponent, selector: "ax-agreement-cell-view", inputs: ["proposal", "agreement", "noAvatar"] }, { kind: "pipe", type: i5.TranslatePipe, name: "translate" }, { kind: "pipe", type: i6.ProposalByIdPipe, name: "proposalById" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ProposalAgreementsSelectSearchComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-proposal-agreements-select-search', template: "<ng-template #option let-result>\n  <ax-agreement-cell-view\n    *ngIf=\"result\"\n    [agreement]=\"result\"\n    [proposal]=\"proposals | proposalById: result.proposalId\"\n  >\n    <div class=\"status\" *ngIf=\"result.agreementData?.confirmed\">\n      <span>\n        {{ result.active ? ('agreement.active' | translate) : ('agreement.expired' | translate) }}\n      </span>\n      <div class=\"status-indicator\" [ngClass]=\"{'is-expired': result.expirationTimestamp}\"></div>\n    </div>\n    <div class=\"status\" *ngIf=\"!result.agreementData?.confirmed\">\n      <span>{{ 'agreement.status.not-confirmed' | translate }}</span>\n      <div class=\"status-indicator is-pending\"></div>\n    </div>\n  </ax-agreement-cell-view>\n</ng-template>\n\n<ng-template #selectedViewTemplate let-result>\n  <ax-agreement-cell-view\n    class=\"selected\"\n    *ngIf=\"result\"\n    [agreement]=\"result\"\n    [noAvatar]=\"true\"\n    [proposal]=\"proposals | proposalById: result.proposalId\"\n  ></ax-agreement-cell-view>\n</ng-template>\n\n<ax-select-search\n  [form]=\"form\"\n  [placeholder]=\"(multiple ? 'label.agreements' : 'label.agreement') | translate\"\n  [optionTemplate]=\"option\"\n  [selectedValueTemplate]=\"selectedViewTemplate\"\n  [multiple]=\"multiple\"\n  [searchCallback$]=\"searchCallback$\"\n  [valueCallback]=\"valueCallback\"\n  [label]=\"label ? label : ((multiple ? 'label.agreements' : 'label.agreement') | translate)\"\n></ax-select-search>\n", styles: [".status{display:flex;align-items:center}.status-indicator{background-color:var(--primary-color-500);width:8px;height:8px;border-radius:6px;margin:0 6px}.status-indicator.is-pending{background-color:#e67e22}.status-indicator.is-expired{background-color:var(--warn-color-500)}.selected{height:38px}\n"] }]
        }], ctorParameters: function () { return [{ type: i1.ProposalsService }]; }, propDecorators: { proposals: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 