import { Component, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { throwError } from 'rxjs';
import { catchError, map, take } from 'rxjs/operators';
import { ShowCondition } from '../../directives/conditional-show/domain/conditional-show.model';
import { HttpError } from '../../domain/http/http-error.model';
import { AlertService } from '../../services/alert/alert.service';
import { OrderService } from '../../services/order/order.service';
import { CaseNotifier } from '../case-editor/services/case.notifier';
import { CaseHistoryService } from './services/case-history.service';
import * as i0 from "@angular/core";
import * as i1 from "@angular/router";
import * as i2 from "../../services/alert/alert.service";
import * as i3 from "../../services/order/order.service";
import * as i4 from "../case-editor/services/case.notifier";
import * as i5 from "./services/case-history.service";
function CaseHistoryComponent_div_0_ng_container_57_ng_container_6_tr_4_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "tr")(1, "th", 14)(2, "div", 15);
    i0.ɵɵtext(3);
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(4, "td");
    i0.ɵɵelement(5, "ccd-field-read", 16);
    i0.ɵɵelementEnd()();
} if (rf & 2) {
    const field_r1 = i0.ɵɵnextContext().$implicit;
    const ctx_r1 = i0.ɵɵnextContext(3);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(field_r1.label);
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("caseField", field_r1)("caseReference", ctx_r1.caseHistory.case_id);
} }
function CaseHistoryComponent_div_0_ng_container_57_ng_container_6_tr_5_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "tr", 17)(1, "th", 18);
    i0.ɵɵelement(2, "ccd-field-read", 16);
    i0.ɵɵelementEnd()();
} if (rf & 2) {
    const field_r1 = i0.ɵɵnextContext().$implicit;
    const ctx_r1 = i0.ɵɵnextContext(3);
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("caseField", field_r1)("caseReference", ctx_r1.caseHistory.case_id);
} }
function CaseHistoryComponent_div_0_ng_container_57_ng_container_6_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "div", 10);
    i0.ɵɵelementContainerStart(2, 11);
    i0.ɵɵpipe(3, "ccdIsCompound");
    i0.ɵɵtemplate(4, CaseHistoryComponent_div_0_ng_container_57_ng_container_6_tr_4_Template, 6, 3, "tr", 12)(5, CaseHistoryComponent_div_0_ng_container_57_ng_container_6_tr_5_Template, 3, 2, "tr", 13);
    i0.ɵɵelementContainerEnd();
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const field_r1 = ctx.$implicit;
    const tab_r3 = i0.ɵɵnextContext().$implicit;
    i0.ɵɵadvance();
    i0.ɵɵproperty("caseField", field_r1)("contextFields", tab_r3.fields)("hidden", field_r1.hidden);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngSwitch", !i0.ɵɵpipeBind1(3, 6, field_r1));
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngSwitchCase", true);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngSwitchCase", false);
} }
function CaseHistoryComponent_div_0_ng_container_57_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "div", 7)(2, "h3", 8);
    i0.ɵɵtext(3);
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(4, "table", 9);
    i0.ɵɵpipe(5, "rpxTranslate");
    i0.ɵɵtemplate(6, CaseHistoryComponent_div_0_ng_container_57_ng_container_6_Template, 6, 8, "ng-container", 6);
    i0.ɵɵpipe(7, "ccdTabFields");
    i0.ɵɵpipe(8, "ccdReadFieldsFilter");
    i0.ɵɵelementEnd()();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const tab_r3 = ctx.$implicit;
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(tab_r3.label);
    i0.ɵɵadvance();
    i0.ɵɵpropertyInterpolate("id", tab_r3.id);
    i0.ɵɵattribute("aria-describedby", i0.ɵɵpipeBind1(5, 4, "case history table"));
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind4(8, 8, i0.ɵɵpipeBind1(7, 6, tab_r3), false, undefined, true));
} }
function CaseHistoryComponent_div_0_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div")(1, "div", 1)(2, "div", 2);
    i0.ɵɵelement(3, "ccd-case-header", 3);
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(4, "div", 1)(5, "div", 2)(6, "div")(7, "h2", 4);
    i0.ɵɵtext(8);
    i0.ɵɵpipe(9, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(10, "table", 5)(11, "tbody")(12, "tr")(13, "th");
    i0.ɵɵtext(14);
    i0.ɵɵpipe(15, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(16, "td");
    i0.ɵɵtext(17);
    i0.ɵɵpipe(18, "ccdDate");
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(19, "tr")(20, "th");
    i0.ɵɵtext(21);
    i0.ɵɵpipe(22, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(23, "td");
    i0.ɵɵtext(24);
    i0.ɵɵpipe(25, "titlecase");
    i0.ɵɵpipe(26, "uppercase");
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(27, "tr")(28, "th");
    i0.ɵɵtext(29);
    i0.ɵɵpipe(30, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(31, "td");
    i0.ɵɵtext(32);
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(33, "tr")(34, "th");
    i0.ɵɵtext(35);
    i0.ɵɵpipe(36, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(37, "td");
    i0.ɵɵtext(38);
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(39, "tr")(40, "th");
    i0.ɵɵtext(41);
    i0.ɵɵpipe(42, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(43, "td");
    i0.ɵɵtext(44);
    i0.ɵɵpipe(45, "ccdDash");
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(46, "tr")(47, "th");
    i0.ɵɵtext(48);
    i0.ɵɵpipe(49, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(50, "td");
    i0.ɵɵtext(51);
    i0.ɵɵpipe(52, "ccdDash");
    i0.ɵɵelementEnd()()()()();
    i0.ɵɵelementStart(53, "div")(54, "h2", 4);
    i0.ɵɵtext(55);
    i0.ɵɵpipe(56, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(57, CaseHistoryComponent_div_0_ng_container_57_Template, 9, 13, "ng-container", 6);
    i0.ɵɵelementEnd()()()();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵadvance(3);
    i0.ɵɵproperty("caseDetails", ctx_r1.caseDetails);
    i0.ɵɵadvance(5);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(9, 17, "Event Details"));
    i0.ɵɵadvance(6);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(15, 19, "Date"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind2(18, 21, ctx_r1.caseHistory.event.timestamp, "local"));
    i0.ɵɵadvance(4);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(22, 24, "Author"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate2("", i0.ɵɵpipeBind1(25, 26, ctx_r1.caseHistory.event.user_first_name), " ", i0.ɵɵpipeBind1(26, 28, ctx_r1.caseHistory.event.user_last_name), "");
    i0.ɵɵadvance(5);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(30, 30, "End state"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(ctx_r1.caseHistory.event.state_name);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(36, 32, "Event"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(ctx_r1.caseHistory.event.event_name);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(42, 34, "Summary"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(45, 36, ctx_r1.caseHistory.event.summary));
    i0.ɵɵadvance(4);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(49, 38, "Comment"));
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(52, 40, ctx_r1.caseHistory.event.comment));
    i0.ɵɵadvance(4);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(56, 42, "Case Details"));
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngForOf", ctx_r1.tabs);
} }
export class CaseHistoryComponent {
    route;
    alertService;
    orderService;
    caseNotifier;
    caseHistoryService;
    static PARAM_EVENT_ID = 'eid';
    static ERROR_MESSAGE = 'No case history to show';
    event;
    caseHistory;
    caseDetails;
    tabs;
    caseSubscription;
    constructor(route, alertService, orderService, caseNotifier, caseHistoryService) {
        this.route = route;
        this.alertService = alertService;
        this.orderService = orderService;
        this.caseNotifier = caseNotifier;
        this.caseHistoryService = caseHistoryService;
    }
    ngOnInit() {
        this.caseSubscription = this.caseNotifier.caseView
            .pipe(take(1))
            .subscribe((caseDetails) => {
            this.caseDetails = caseDetails;
            const eventId = this.route.snapshot.paramMap.get(CaseHistoryComponent.PARAM_EVENT_ID) || this.event;
            this.caseHistoryService
                .get(this.caseDetails.case_id, eventId)
                .pipe(map(caseHistory => {
                if (!caseHistory) {
                    const error = new HttpError();
                    error.message = CaseHistoryComponent.ERROR_MESSAGE;
                    throw error;
                }
                this.caseHistory = caseHistory;
                this.tabs = this.orderService.sort(this.caseHistory.tabs);
                this.tabs = this.sortTabFieldsAndFilterTabs(this.tabs);
            }), catchError(error => {
                console.error(error);
                if (error.status !== 401 && error.status !== 403) {
                    this.alertService.error(error.message);
                }
                return throwError(error);
            })).toPromise();
        });
    }
    ngOnDestroy() {
        if (this.caseSubscription) {
            this.caseSubscription.unsubscribe();
        }
    }
    isDataLoaded() {
        return !!(this.caseDetails && this.caseHistory);
    }
    sortTabFieldsAndFilterTabs(tabs) {
        return tabs
            .map(tab => Object.assign({}, tab, { fields: this.orderService.sort(tab.fields) }))
            .filter(tab => ShowCondition.getInstance(tab.show_condition).matchByContextFields(tab.fields));
    }
    static ɵfac = function CaseHistoryComponent_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || CaseHistoryComponent)(i0.ɵɵdirectiveInject(i1.ActivatedRoute), i0.ɵɵdirectiveInject(i2.AlertService), i0.ɵɵdirectiveInject(i3.OrderService), i0.ɵɵdirectiveInject(i4.CaseNotifier), i0.ɵɵdirectiveInject(i5.CaseHistoryService)); };
    static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: CaseHistoryComponent, selectors: [["ccd-case-history"]], inputs: { event: "event" }, decls: 1, vars: 1, consts: [[4, "ngIf"], [1, "grid-row"], [1, "column-full"], [3, "caseDetails"], [1, "heading-h2"], ["aria-describedby", "event details table", 1, "EventDetails"], [4, "ngFor", "ngForOf"], [1, "caseHistorySection"], [1, "heading-h3"], [1, "CaseHistory", 3, "id"], ["ccdLabelSubstitutor", "", 3, "caseField", "contextFields", "hidden"], [3, "ngSwitch"], [4, "ngSwitchCase"], ["class", "compound-field", 4, "ngSwitchCase"], ["id", "case-viewer-label-header"], [1, "case-viewer-label"], [3, "caseField", "caseReference"], [1, "compound-field"], ["colspan", "2"]], template: function CaseHistoryComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵtemplate(0, CaseHistoryComponent_div_0_Template, 58, 44, "div", 0);
        } if (rf & 2) {
            i0.ɵɵproperty("ngIf", ctx.isDataLoaded());
        } }, styles: [".CaseHistory[_ngcontent-%COMP%]   th[_ngcontent-%COMP%], .CaseHistory[_ngcontent-%COMP%]   td[_ngcontent-%COMP%]{border-bottom:none}.caseHistorySection[_ngcontent-%COMP%]{margin-top:40px}.EventDetails[_ngcontent-%COMP%]   th[_ngcontent-%COMP%], .EventDetails[_ngcontent-%COMP%]   td[_ngcontent-%COMP%]{border-bottom:none}th[_ngcontent-%COMP%]{width:1%;white-space:nowrap;vertical-align:top}.compound-field[_ngcontent-%COMP%]   td[_ngcontent-%COMP%]{padding:0}.case-viewer-controls[_ngcontent-%COMP%]{margin-top:47px;margin-bottom:20px}ccd-case-header[_ngcontent-%COMP%]{float:left;margin-right:10px}ccd-event-trigger[_ngcontent-%COMP%]{float:right}.case-viewer-label[_ngcontent-%COMP%]{min-width:300px;white-space:normal}"] });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(CaseHistoryComponent, [{
        type: Component,
        args: [{ selector: 'ccd-case-history', template: "<div *ngIf=\"isDataLoaded()\">\n  <div class=\"grid-row\">\n    <div class=\"column-full\">\n      <ccd-case-header [caseDetails]=\"caseDetails\"></ccd-case-header>\n    </div>\n  </div>\n  <div class=\"grid-row\">\n    <div class=\"column-full\">\n      <div>\n        <h2 class=\"heading-h2\">{{'Event Details' | rpxTranslate}}</h2>\n        <table class=\"EventDetails\" aria-describedby=\"event details table\">\n          <tbody>\n          <tr>\n            <th>{{'Date' | rpxTranslate}}</th>\n            <td>{{caseHistory.event.timestamp | ccdDate : 'local'}}</td>\n          </tr>\n          <tr>\n            <th>{{'Author' | rpxTranslate}}</th>\n            <td>{{caseHistory.event.user_first_name | titlecase}} {{caseHistory.event.user_last_name | uppercase}}</td>\n          </tr>\n          <tr>\n            <th>{{'End state' | rpxTranslate}}</th>\n            <td>{{caseHistory.event.state_name}}</td>\n          </tr>\n          <tr>\n            <th>{{'Event' | rpxTranslate}}</th>\n            <td>{{caseHistory.event.event_name}}</td>\n          </tr>\n          <tr>\n            <th>{{'Summary' | rpxTranslate}}</th>\n            <td>{{caseHistory.event.summary | ccdDash}}</td>\n          </tr>\n          <tr>\n            <th>{{'Comment' | rpxTranslate}}</th>\n            <td>{{caseHistory.event.comment | ccdDash}}</td>\n          </tr>\n          </tbody>\n        </table>\n      </div>\n      <div>\n        <h2 class=\"heading-h2\">{{'Case Details' | rpxTranslate}}</h2>\n        <ng-container *ngFor=\"let tab of tabs\">\n          <div class=\"caseHistorySection\">\n            <h3 class=\"heading-h3\">{{tab.label}}</h3>\n            <table class=\"CaseHistory\" id=\"{{tab.id}}\" [attr.aria-describedby]=\"'case history table' | rpxTranslate\">\n              <ng-container *ngFor=\"let field of tab | ccdTabFields | ccdReadFieldsFilter:false :undefined :true\">\n                <div ccdLabelSubstitutor [caseField]=\"field\" [contextFields]=\"tab.fields\" [hidden]=\"field.hidden\">\n                  <ng-container [ngSwitch]=\"!(field | ccdIsCompound)\">\n                    <tr *ngSwitchCase=\"true\">\n                      <th id=\"case-viewer-label-header\">\n                        <div class=\"case-viewer-label\">{{field.label}}</div>\n                      </th>\n                      <td>\n                        <ccd-field-read [caseField]=\"field\" [caseReference]=\"caseHistory.case_id\"></ccd-field-read>\n                      </td>\n                    </tr>\n                    <tr *ngSwitchCase=\"false\" class=\"compound-field\">\n                      <th colspan=\"2\">\n                        <ccd-field-read [caseField]=\"field\" [caseReference]=\"caseHistory.case_id\"></ccd-field-read>\n                      </th>\n                    </tr>\n                  </ng-container>\n                </div>\n              </ng-container>\n            </table>\n          </div>\n        </ng-container>\n      </div>\n    </div>\n  </div>\n</div>\n", styles: [".CaseHistory th,.CaseHistory td{border-bottom:none}.caseHistorySection{margin-top:40px}.EventDetails th,.EventDetails td{border-bottom:none}th{width:1%;white-space:nowrap;vertical-align:top}.compound-field td{padding:0}.case-viewer-controls{margin-top:47px;margin-bottom:20px}ccd-case-header{float:left;margin-right:10px}ccd-event-trigger{float:right}.case-viewer-label{min-width:300px;white-space:normal}\n"] }]
    }], () => [{ type: i1.ActivatedRoute }, { type: i2.AlertService }, { type: i3.OrderService }, { type: i4.CaseNotifier }, { type: i5.CaseHistoryService }], { event: [{
            type: Input
        }] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(CaseHistoryComponent, { className: "CaseHistoryComponent", filePath: "lib/shared/components/case-history/case-history.component.ts", lineNumber: 20 }); })();
//# sourceMappingURL=data:application/json;base64, 