var __decorate = (this && this.__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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { FieldsUtils, FieldsPurger } from '../../../services/fields';
import { ConditionalShowRegistrarService, GreyBarService } from '../../../directives';
import { WizardFactoryService } from '../services/wizard-factory.service';
import { CaseEventTrigger } from '../../../domain/case-view/case-event-trigger.model';
import { CaseView } from '../../../domain/case-view/case-view.model';
import { ProfileService, ProfileNotifier } from '../../../services';
var CaseEditComponent = /** @class */ (function () {
    function CaseEditComponent(fb, router, route, fieldsUtils, fieldsPurger, registrarService, wizardFactory, profileService, profileNotifier) {
        this.fb = fb;
        this.router = router;
        this.route = route;
        this.fieldsUtils = fieldsUtils;
        this.fieldsPurger = fieldsPurger;
        this.registrarService = registrarService;
        this.wizardFactory = wizardFactory;
        this.profileService = profileService;
        this.profileNotifier = profileNotifier;
        this.cancelled = new EventEmitter();
        this.submitted = new EventEmitter();
    }
    CaseEditComponent_1 = CaseEditComponent;
    CaseEditComponent.prototype.ngOnInit = function () {
        var _this = this;
        this.wizard = this.wizardFactory.create(this.eventTrigger);
        this.form = this.fb.group({
            'data': new FormGroup({}),
            'event': this.fb.group({
                'id': [this.eventTrigger.id, Validators.required],
                'summary': [''],
                'description': ['']
            })
        });
        this.route.queryParams.subscribe(function (params) {
            _this.navigationOrigin = params[CaseEditComponent_1.ORIGIN_QUERY_PARAM];
        });
        this.announceProfile(this.route);
    };
    CaseEditComponent.prototype.getPage = function (pageId) {
        return this.wizard.getPage(pageId, this.fieldsUtils.buildCanShowPredicate(this.eventTrigger, this.form));
    };
    CaseEditComponent.prototype.first = function () {
        var firstPage = this.wizard.firstPage(this.fieldsUtils.buildCanShowPredicate(this.eventTrigger, this.form));
        return this.router.navigate([firstPage ? firstPage.id : 'submit'], { relativeTo: this.route });
    };
    CaseEditComponent.prototype.navigateToPage = function (pageId) {
        var page = this.getPage(pageId);
        return this.router.navigate([page ? page.id : 'submit'], { relativeTo: this.route });
    };
    CaseEditComponent.prototype.next = function (currentPageId) {
        this.fieldsPurger.clearHiddenFields(this.form, this.wizard, this.eventTrigger, currentPageId);
        this.registrarService.reset();
        var theQueryParams = {};
        theQueryParams[CaseEditComponent_1.ORIGIN_QUERY_PARAM] = this.navigationOrigin;
        var nextPage = this.wizard.nextPage(currentPageId, this.fieldsUtils.buildCanShowPredicate(this.eventTrigger, this.form));
        return this.router.navigate([nextPage ? nextPage.id : 'submit'], { queryParams: theQueryParams, relativeTo: this.route });
    };
    CaseEditComponent.prototype.previous = function (currentPageId) {
        this.fieldsPurger.clearHiddenFields(this.form, this.wizard, this.eventTrigger, currentPageId);
        this.registrarService.reset();
        var previousPage = this.wizard.previousPage(currentPageId, this.fieldsUtils.buildCanShowPredicate(this.eventTrigger, this.form));
        if (!previousPage) {
            return Promise.resolve(false);
        }
        var theQueryParams = {};
        theQueryParams[CaseEditComponent_1.ORIGIN_QUERY_PARAM] = this.navigationOrigin;
        return this.router.navigate([previousPage.id], { queryParams: theQueryParams, relativeTo: this.route });
    };
    CaseEditComponent.prototype.hasPrevious = function (currentPageId) {
        return this.wizard.hasPreviousPage(currentPageId, this.fieldsUtils.buildCanShowPredicate(this.eventTrigger, this.form));
    };
    CaseEditComponent.prototype.cancel = function () {
        this.cancelled.emit();
    };
    CaseEditComponent.prototype.confirm = function (confirmation) {
        this.confirmation = confirmation;
        return this.router.navigate(['confirm'], { relativeTo: this.route });
    };
    CaseEditComponent.prototype.announceProfile = function (route) {
        var _this = this;
        route.snapshot.pathFromRoot[1].data.profile ?
            this.profileNotifier.announceProfile(route.snapshot.pathFromRoot[1].data.profile)
            : this.profileService.get().subscribe(function (_) { return _this.profileNotifier.announceProfile(_); });
    };
    var CaseEditComponent_1;
    CaseEditComponent.ORIGIN_QUERY_PARAM = 'origin';
    __decorate([
        Input(),
        __metadata("design:type", CaseEventTrigger)
    ], CaseEditComponent.prototype, "eventTrigger", void 0);
    __decorate([
        Input(),
        __metadata("design:type", Function)
    ], CaseEditComponent.prototype, "submit", void 0);
    __decorate([
        Input(),
        __metadata("design:type", Function)
    ], CaseEditComponent.prototype, "validate", void 0);
    __decorate([
        Input(),
        __metadata("design:type", Function)
    ], CaseEditComponent.prototype, "saveDraft", void 0);
    __decorate([
        Input(),
        __metadata("design:type", CaseView)
    ], CaseEditComponent.prototype, "caseDetails", void 0);
    __decorate([
        Output(),
        __metadata("design:type", EventEmitter)
    ], CaseEditComponent.prototype, "cancelled", void 0);
    __decorate([
        Output(),
        __metadata("design:type", EventEmitter)
    ], CaseEditComponent.prototype, "submitted", void 0);
    CaseEditComponent = CaseEditComponent_1 = __decorate([
        Component({
            selector: 'ccd-case-edit',
            template: "\n    <router-outlet></router-outlet>\n  ",
            styles: ["\n    #fieldset-case-data{margin-bottom:30px}#fieldset-case-data th{width:1%;white-space:nowrap;vertical-align:top}.compound-field td{padding:0}#confirmation-header{width:630px;background-color:#17958b;border:solid 1px #979797;color:#ffffff;text-align:center}#confirmation-body{width:630px;background-color:#ffffff}.valign-top{vertical-align:top}.summary-fields{margin-bottom:30px}.summary-fields tbody tr th,.summary-fields tbody tr td{border-bottom:0px}a.disabled{pointer-events:none;cursor:default}\n  "],
            providers: [GreyBarService]
        }),
        __metadata("design:paramtypes", [FormBuilder,
            Router,
            ActivatedRoute,
            FieldsUtils,
            FieldsPurger,
            ConditionalShowRegistrarService,
            WizardFactoryService,
            ProfileService,
            ProfileNotifier])
    ], CaseEditComponent);
    return CaseEditComponent;
}());
export { CaseEditComponent };
//# sourceMappingURL=case-edit.component.js.map