import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { SessionStorageService } from '../../../services';
import { isInternalUser, isJudiciaryUser } from '../../../utils';
import { AbstractFieldReadComponent } from '../base-field/abstract-field-read.component';
import { PaletteContext } from '../base-field/palette-context.enum';
import { QueryCreateContext } from './models';
import { QueryManagementUtils } from './utils/query-management.utils';
import { CaseNotifier } from '../../case-editor/services/case.notifier';
import { AbstractAppConfig } from '../../../../app.config';
import * as i0 from "@angular/core";
import * as i1 from "@angular/router";
import * as i2 from "../../../services";
import * as i3 from "../../case-editor/services/case.notifier";
import * as i4 from "../../../../app.config";
const _c0 = (a0, a1) => ["/query-management", "query", a0, "4", a1];
function ReadQueryManagementFieldComponent_ng_container_0_ng_container_1_div_1_Template(rf, ctx) { if (rf & 1) {
    const _r1 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "div", 6)(1, "ccd-query-list", 7);
    i0.ɵɵlistener("selectedQuery", function ReadQueryManagementFieldComponent_ng_container_0_ng_container_1_div_1_Template_ccd_query_list_selectedQuery_1_listener($event) { i0.ɵɵrestoreView(_r1); const ctx_r1 = i0.ɵɵnextContext(3); return i0.ɵɵresetView(ctx_r1.setQuery($event)); });
    i0.ɵɵelementEnd()();
} if (rf & 2) {
    const caseQueriesCollection_r3 = i0.ɵɵnextContext().$implicit;
    i0.ɵɵadvance();
    i0.ɵɵproperty("caseQueriesCollection", caseQueriesCollection_r3);
} }
function ReadQueryManagementFieldComponent_ng_container_0_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, ReadQueryManagementFieldComponent_ng_container_0_ng_container_1_div_1_Template, 2, 1, "div", 5);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext(2);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", ctx_r1.showQueryList);
} }
function ReadQueryManagementFieldComponent_ng_container_0_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, ReadQueryManagementFieldComponent_ng_container_0_ng_container_1_Template, 2, 1, "ng-container", 4);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngForOf", ctx_r1.caseQueriesCollections);
} }
function ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "button", 10);
    i0.ɵɵtext(2);
    i0.ɵɵpipe(3, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext(3);
    i0.ɵɵadvance();
    i0.ɵɵproperty("routerLink", i0.ɵɵpureFunction2(4, _c0, ctx_r1.caseId, ctx_r1.query.id));
    i0.ɵɵadvance();
    i0.ɵɵtextInterpolate1(" ", i0.ɵɵpipeBind1(3, 2, "Ask a follow-up question"), " ");
} }
function ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_template_2_ng_container_0_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "button", 10);
    i0.ɵɵtext(2);
    i0.ɵɵpipe(3, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext(4);
    i0.ɵɵadvance();
    i0.ɵɵproperty("routerLink", i0.ɵɵpureFunction2(4, _c0, ctx_r1.caseId, ctx_r1.query.id));
    i0.ɵɵadvance();
    i0.ɵɵtextInterpolate1(" ", i0.ɵɵpipeBind1(3, 2, "Ask a follow-up question"), " ");
} }
function ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_template_2_ng_template_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div")(1, "p", 11);
    i0.ɵɵtext(2);
    i0.ɵɵpipe(3, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(4, "p");
    i0.ɵɵtext(5);
    i0.ɵɵpipe(6, "rpxTranslate");
    i0.ɵɵelementEnd()();
} if (rf & 2) {
    i0.ɵɵadvance(2);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(3, 2, "Your query is under review"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(6, 4, "Our team will read your query and respond. Do not submit the same query more than once."));
} }
function ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_template_2_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵtemplate(0, ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_template_2_ng_container_0_Template, 4, 7, "ng-container", 3)(1, ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_template_2_ng_template_1_Template, 7, 6, "ng-template", null, 2, i0.ɵɵtemplateRefExtractor);
} if (rf & 2) {
    const queryIsInReview_r5 = i0.ɵɵreference(2);
    const ctx_r1 = i0.ɵɵnextContext(3);
    i0.ɵɵproperty("ngIf", (ctx_r1.query == null ? null : ctx_r1.query.children == null ? null : ctx_r1.query.children.length) > 0 && (ctx_r1.query == null ? null : ctx_r1.query.children == null ? null : ctx_r1.query.children.length) % 2 === 1)("ngIfElse", queryIsInReview_r5);
} }
function ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_container_1_Template, 4, 7, "ng-container", 3)(2, ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_ng_template_2_Template, 3, 2, "ng-template", null, 1, i0.ɵɵtemplateRefExtractor);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const sequentialQuery_r6 = i0.ɵɵreference(3);
    const ctx_r1 = i0.ɵɵnextContext(2);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", ctx_r1.messageType && ctx_r1.messageType === ctx_r1.followUpQuery && ctx_r1.isMultipleFollowUpEnabled || ctx_r1.messageType && ctx_r1.messageType === ctx_r1.respondToQuery)("ngIfElse", sequentialQuery_r6);
} }
function ReadQueryManagementFieldComponent_ng_template_1_ng_container_2_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "div", 12)(2, "span", 13);
    i0.ɵɵtext(3, "!");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(4, "strong", 14)(5, "span", 15);
    i0.ɵɵtext(6, "Warning");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(7, "p", 16);
    i0.ɵɵtext(8);
    i0.ɵɵpipe(9, "rpxTranslate");
    i0.ɵɵelementEnd()()();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    i0.ɵɵadvance(8);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(9, 1, "This query has been closed by HMCTS staff."));
} }
function ReadQueryManagementFieldComponent_ng_template_1_Template(rf, ctx) { if (rf & 1) {
    const _r4 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "ccd-query-details", 8);
    i0.ɵɵlistener("backClicked", function ReadQueryManagementFieldComponent_ng_template_1_Template_ccd_query_details_backClicked_0_listener() { i0.ɵɵrestoreView(_r4); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.showQueryList = true); });
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(1, ReadQueryManagementFieldComponent_ng_template_1_ng_container_1_Template, 4, 2, "ng-container", 9)(2, ReadQueryManagementFieldComponent_ng_template_1_ng_container_2_Template, 10, 3, "ng-container", 9);
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵproperty("query", ctx_r1.query)("caseId", ctx_r1.caseId);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", !ctx_r1.isInternalUser() && !ctx_r1.isQueryClosed && !ctx_r1.isJudiciaryUser());
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", ctx_r1.isQueryClosed);
} }
export class ReadQueryManagementFieldComponent extends AbstractFieldReadComponent {
    route;
    sessionStorageService;
    caseNotifier;
    abstractConfig;
    caseQueriesCollections;
    query;
    showQueryList = true;
    caseId;
    messageType;
    followUpQuery = QueryCreateContext.FOLLOWUP;
    respondToQuery = QueryCreateContext.RESPOND;
    isQueryClosed = false;
    value;
    isMultipleFollowUpEnabled = false;
    currentJurisdictionId;
    enableServiceSpecificMultiFollowups = [];
    caseSubscription;
    constructor(route, sessionStorageService, caseNotifier, abstractConfig) {
        super();
        this.route = route;
        this.sessionStorageService = sessionStorageService;
        this.caseNotifier = caseNotifier;
        this.abstractConfig = abstractConfig;
    }
    ngOnInit() {
        this.caseId = this.route.snapshot.params.cid;
        this.enableServiceSpecificMultiFollowups = this.abstractConfig.getEnableServiceSpecificMultiFollowups() || [];
        this.caseSubscription = this.caseNotifier.caseView.subscribe((caseDetails) => {
            if (caseDetails?.case_type?.jurisdiction?.id) {
                this.currentJurisdictionId = caseDetails.case_type.jurisdiction.id;
                this.isMultipleFollowUpEnabled = this.enableServiceSpecificMultiFollowups.includes(this.currentJurisdictionId);
            }
        });
        if (this.context === PaletteContext.DEFAULT) {
            // EUI-8303 Using mock data until CCD is ready with the API and data contract
            // this.caseQueriesCollections = caseMessagesMockData;
            // TODO: Actual implementation once the CCD API and data contract is available
            // Each parties will have a separate collection of party messages
            // Find whether queries tab is available in the case data
            this.caseNotifier.fetchAndRefresh(this.caseId)
                .subscribe({
                next: (caseDetails) => {
                    if (this.route.snapshot.data.case?.tabs) {
                        this.caseQueriesCollections = caseDetails.tabs
                            .filter((tab) => tab.fields?.some((caseField) => caseField.field_type.type === 'ComponentLauncher' && caseField.id === this.caseField.id))[0].fields?.reduce((acc, caseField) => {
                            const extractedCaseQueriesFromCaseField = QueryManagementUtils.extractCaseQueriesFromCaseField(caseField);
                            if (extractedCaseQueriesFromCaseField && typeof extractedCaseQueriesFromCaseField === 'object') {
                                acc.push(extractedCaseQueriesFromCaseField);
                            }
                            return acc;
                        }, []);
                    }
                }
            });
            // Loop through the list of parties and their case queries collections
            // QueryManagementUtils.extractCaseQueriesFromCaseField();
        }
    }
    ngOnDestroy() {
        this.caseSubscription?.unsubscribe();
    }
    setQuery(query) {
        this.showQueryList = false;
        this.query = query;
        this.messageType = this.getMessageType(query);
        this.isQueryClosed = this.query?.children?.some((queryItem) => queryItem?.isClosed === 'Yes');
    }
    backToQueryListPage() {
        this.showQueryList = true;
        this.query = null;
    }
    isInternalUser() {
        return isInternalUser(this.sessionStorageService);
    }
    isJudiciaryUser() {
        return isJudiciaryUser(this.sessionStorageService);
    }
    getMessageType(query) {
        if (!query) {
            return undefined;
        }
        return query.children?.length
            ? query.children[query.children.length - 1]?.messageType
            : query?.messageType;
    }
    static ɵfac = function ReadQueryManagementFieldComponent_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || ReadQueryManagementFieldComponent)(i0.ɵɵdirectiveInject(i1.ActivatedRoute), i0.ɵɵdirectiveInject(i2.SessionStorageService), i0.ɵɵdirectiveInject(i3.CaseNotifier), i0.ɵɵdirectiveInject(i4.AbstractAppConfig)); };
    static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: ReadQueryManagementFieldComponent, selectors: [["ccd-read-query-management-field"]], features: [i0.ɵɵInheritDefinitionFeature], decls: 3, vars: 2, consts: [["singleQueryDetails", ""], ["sequentialQuery", ""], ["queryIsInReview", ""], [4, "ngIf", "ngIfElse"], [4, "ngFor", "ngForOf"], ["class", "govuk-!-margin-top-8 govuk-!-margin-bottom-8", 4, "ngIf"], [1, "govuk-!-margin-top-8", "govuk-!-margin-bottom-8"], [3, "selectedQuery", "caseQueriesCollection"], [3, "backClicked", "query", "caseId"], [4, "ngIf"], ["id", "ask-follow-up-question", "data-module", "govuk-button", 1, "govuk-button", 3, "routerLink"], [1, "govuk-!-font-weight-bold"], [1, "govuk-warning-text"], ["aria-hidden", "true", 1, "govuk-warning-text__icon"], [1, "govuk-warning-text__text"], [1, "govuk-visually-hidden"], [1, "qm-service-message", "govuk-!-font-weight-bold"]], template: function ReadQueryManagementFieldComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵtemplate(0, ReadQueryManagementFieldComponent_ng_container_0_Template, 2, 1, "ng-container", 3)(1, ReadQueryManagementFieldComponent_ng_template_1_Template, 3, 4, "ng-template", null, 0, i0.ɵɵtemplateRefExtractor);
        } if (rf & 2) {
            const singleQueryDetails_r7 = i0.ɵɵreference(2);
            i0.ɵɵproperty("ngIf", ctx.showQueryList)("ngIfElse", singleQueryDetails_r7);
        } }, encapsulation: 2 });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ReadQueryManagementFieldComponent, [{
        type: Component,
        args: [{ selector: 'ccd-read-query-management-field', template: "<ng-container *ngIf=\"showQueryList; else singleQueryDetails\">\n  <ng-container *ngFor=\"let caseQueriesCollection of caseQueriesCollections\">\n    <div *ngIf=\"showQueryList\" class=\"govuk-!-margin-top-8 govuk-!-margin-bottom-8\">\n      <ccd-query-list (selectedQuery)=\"setQuery($event)\" [caseQueriesCollection]=\"caseQueriesCollection\"></ccd-query-list>\n    </div>\n  </ng-container>\n</ng-container>\n\n<ng-template #singleQueryDetails>\n  <ccd-query-details\n    [query]=\"query\"\n    (backClicked)=\"showQueryList = true\"\n    [caseId]=\"caseId\"\n  ></ccd-query-details>\n\n  <ng-container *ngIf=\"!isInternalUser() && !isQueryClosed && !isJudiciaryUser()\">\n    <ng-container *ngIf=\"(messageType && messageType === followUpQuery  && isMultipleFollowUpEnabled) || (messageType && messageType === respondToQuery); else sequentialQuery\">\n      <button id=\"ask-follow-up-question\" class=\"govuk-button\" data-module=\"govuk-button\"\n              [routerLink]=\"['/query-management', 'query', caseId, '4', query.id]\">\n        {{ 'Ask a follow-up question' | rpxTranslate }}\n      </button>\n    </ng-container>\n    <ng-template #sequentialQuery>\n      <ng-container *ngIf=\"query?.children?.length > 0 && query?.children?.length % 2 === 1; else queryIsInReview\">\n        <button id=\"ask-follow-up-question\" class=\"govuk-button\" data-module=\"govuk-button\"\n                [routerLink]=\"['/query-management', 'query', caseId, '4', query.id]\">\n          {{ 'Ask a follow-up question' | rpxTranslate }}\n        </button>\n      </ng-container>\n\n      <ng-template #queryIsInReview>\n        <div>\n          <p class=\"govuk-!-font-weight-bold\">{{ 'Your query is under review' | rpxTranslate }}</p>\n          <p>{{ 'Our team will read your query and respond. Do not submit the same query more than once.' | rpxTranslate }}</p>\n        </div>\n      </ng-template>\n    </ng-template>\n\n  </ng-container>\n  <ng-container *ngIf=\"isQueryClosed\">\n    <div class=\"govuk-warning-text\">\n      <span aria-hidden=\"true\" class=\"govuk-warning-text__icon\">!</span>\n      <strong class=\"govuk-warning-text__text\">\n        <span class=\"govuk-visually-hidden\">Warning</span>\n        <p class=\"qm-service-message govuk-!-font-weight-bold\">{{ 'This query has been closed by HMCTS staff.' | rpxTranslate }}</p>\n      </strong>\n    </div>\n  </ng-container>\n</ng-template>\n" }]
    }], () => [{ type: i1.ActivatedRoute }, { type: i2.SessionStorageService }, { type: i3.CaseNotifier }, { type: i4.AbstractAppConfig }], null); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(ReadQueryManagementFieldComponent, { className: "ReadQueryManagementFieldComponent", filePath: "lib/shared/components/palette/query-management/read-query-management-field.component.ts", lineNumber: 18 }); })();
//# sourceMappingURL=data:application/json;base64, 