import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Validators } from '@angular/forms';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { getCustomFieldsDefaults, TypedBaseDetailComponent, } from '@vendure/admin-ui/core';
import { gql } from 'apollo-angular';
import { mergeMap, take } from 'rxjs/operators';
import * as i0 from "@angular/core";
import * as i1 from "@vendure/admin-ui/core";
import * as i2 from "@angular/forms";
import * as i3 from "@angular/common";
import * as i4 from "@ngx-translate/core";
export const GET_PROFILE_DETAIL = gql `
    query GetProfileDetail {
        activeAdministrator {
            ...ProfileDetail
        }
    }
    fragment ProfileDetail on Administrator {
        id
        createdAt
        updatedAt
        firstName
        lastName
        emailAddress
        user {
            id
            lastLogin
            verified
        }
    }
`;
export class ProfileComponent extends TypedBaseDetailComponent {
    constructor(changeDetector, dataService, formBuilder, notificationService) {
        super();
        this.changeDetector = changeDetector;
        this.dataService = dataService;
        this.formBuilder = formBuilder;
        this.notificationService = notificationService;
        this.customFields = this.getCustomFieldConfig('Administrator');
        this.detailForm = this.formBuilder.group({
            emailAddress: ['', Validators.required],
            firstName: ['', Validators.required],
            lastName: ['', Validators.required],
            password: [''],
            customFields: this.formBuilder.group(getCustomFieldsDefaults(this.customFields)),
        });
    }
    ngOnInit() {
        this.init();
    }
    ngOnDestroy() {
        this.destroy();
    }
    save() {
        this.entity$
            .pipe(take(1), mergeMap(({ id }) => {
            const formValue = this.detailForm.value;
            const administrator = {
                emailAddress: formValue.emailAddress,
                firstName: formValue.firstName,
                lastName: formValue.lastName,
                password: formValue.password,
                customFields: formValue.customFields,
            };
            return this.dataService.administrator.updateActiveAdministrator(administrator);
        }))
            .subscribe(data => {
            this.notificationService.success(_('common.notify-update-success'), {
                entity: 'Administrator',
            });
            this.detailForm.markAsPristine();
            this.changeDetector.markForCheck();
        }, err => {
            this.notificationService.error(_('common.notify-update-error'), {
                entity: 'Administrator',
            });
        });
    }
    setFormValues(administrator, languageCode) {
        this.detailForm.patchValue({
            emailAddress: administrator.emailAddress,
            firstName: administrator.firstName,
            lastName: administrator.lastName,
        });
        if (this.customFields.length) {
            this.setCustomFieldFormValues(this.customFields, this.detailForm.get('customFields'), administrator);
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: ProfileComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i1.DataService }, { token: i2.FormBuilder }, { token: i1.NotificationService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.2", type: ProfileComponent, selector: "vdr-profile", usesInheritance: true, ngImport: i0, template: "<vdr-page-block>\n    <vdr-action-bar>\n        <vdr-ab-left></vdr-ab-left>\n        <vdr-ab-right>\n            <vdr-action-bar-items locationId=\"profile\"></vdr-action-bar-items>\n            <button\n                class=\"btn btn-primary\"\n                (click)=\"save()\"\n                [disabled]=\"detailForm.invalid || detailForm.pristine\"\n            >\n                {{ 'common.update' | translate }}\n            </button>\n        </vdr-ab-right>\n    </vdr-action-bar>\n</vdr-page-block>\n\n<form class=\"form\" [formGroup]=\"detailForm\">\n    <vdr-page-detail-layout>\n        <vdr-page-detail-sidebar>\n            <vdr-card *ngIf=\"entity$ | async as entity\">\n                <vdr-page-entity-info [entity]=\"entity\" />\n            </vdr-card>\n        </vdr-page-detail-sidebar>\n        <vdr-page-block>\n            <vdr-card>\n                <div class=\"form-grid\">\n                    <vdr-form-field [label]=\"'settings.email-address' | translate\" for=\"emailAddress\">\n                        <input id=\"emailAddress\" type=\"text\" formControlName=\"emailAddress\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'settings.first-name' | translate\" for=\"firstName\">\n                        <input id=\"firstName\" type=\"text\" formControlName=\"firstName\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'settings.last-name' | translate\" for=\"lastName\">\n                        <input id=\"lastName\" type=\"text\" formControlName=\"lastName\" />\n                    </vdr-form-field>\n                    <vdr-form-field\n                        *ngIf=\"isNew$ | async\"\n                        [label]=\"'settings.password' | translate\"\n                        for=\"password\"\n                    >\n                        <input id=\"password\" type=\"password\" formControlName=\"password\" />\n                    </vdr-form-field>\n                    <vdr-form-field\n                        [label]=\"'settings.password' | translate\"\n                        for=\"password\"\n                        [readOnlyToggle]=\"true\"\n                    >\n                        <input id=\"password\" type=\"password\" formControlName=\"password\" />\n                    </vdr-form-field>\n                </div>\n            </vdr-card>\n            <vdr-card\n                formGroupName=\"customFields\"\n                *ngIf=\"customFields.length\"\n                [title]=\"'common.custom-fields' | translate\"\n            >\n                <vdr-tabbed-custom-fields\n                    entityName=\"Administrator\"\n                    [customFields]=\"customFields\"\n                    [customFieldsFormGroup]=\"detailForm.get('customFields')\"\n                ></vdr-tabbed-custom-fields>\n            </vdr-card>\n            <vdr-custom-detail-component-host\n                locationId=\"administrator-profile\"\n                [entity$]=\"entity$\"\n                [detailForm]=\"detailForm\"\n            ></vdr-custom-detail-component-host>\n        </vdr-page-block>\n    </vdr-page-detail-layout>\n</form>\n", styles: [""], dependencies: [{ kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i2.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i2.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i2.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i2.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { kind: "directive", type: i2.FormGroupName, selector: "[formGroupName]", inputs: ["formGroupName"] }, { kind: "component", type: i1.ActionBarComponent, selector: "vdr-action-bar" }, { kind: "component", type: i1.ActionBarLeftComponent, selector: "vdr-ab-left", inputs: ["grow"] }, { kind: "component", type: i1.ActionBarRightComponent, selector: "vdr-ab-right", inputs: ["grow"] }, { kind: "component", type: i1.FormFieldComponent, selector: "vdr-form-field", inputs: ["label", "for", "tooltip", "errors", "readOnlyToggle"] }, { kind: "directive", type: i1.FormFieldControlDirective, selector: "input, textarea, select" }, { kind: "component", type: i1.ActionBarItemsComponent, selector: "vdr-action-bar-items", inputs: ["locationId"] }, { kind: "component", type: i1.TabbedCustomFieldsComponent, selector: "vdr-tabbed-custom-fields", inputs: ["entityName", "customFields", "customFieldsFormGroup", "readonly", "compact", "showLabel"] }, { kind: "component", type: i1.CustomDetailComponentHostComponent, selector: "vdr-custom-detail-component-host", inputs: ["locationId", "entity$", "detailForm"] }, { kind: "component", type: i1.PageBlockComponent, selector: "vdr-page-block" }, { kind: "component", type: i1.PageEntityInfoComponent, selector: "vdr-page-entity-info", inputs: ["entity"] }, { kind: "component", type: i1.PageDetailLayoutComponent, selector: "vdr-page-detail-layout" }, { kind: "component", type: i1.PageDetailSidebarComponent, selector: "vdr-page-detail-sidebar" }, { kind: "component", type: i1.CardComponent, selector: "vdr-card", inputs: ["title", "paddingX"] }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }, { kind: "pipe", type: i4.TranslatePipe, name: "translate" }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: ProfileComponent, decorators: [{
            type: Component,
            args: [{ selector: 'vdr-profile', changeDetection: ChangeDetectionStrategy.OnPush, template: "<vdr-page-block>\n    <vdr-action-bar>\n        <vdr-ab-left></vdr-ab-left>\n        <vdr-ab-right>\n            <vdr-action-bar-items locationId=\"profile\"></vdr-action-bar-items>\n            <button\n                class=\"btn btn-primary\"\n                (click)=\"save()\"\n                [disabled]=\"detailForm.invalid || detailForm.pristine\"\n            >\n                {{ 'common.update' | translate }}\n            </button>\n        </vdr-ab-right>\n    </vdr-action-bar>\n</vdr-page-block>\n\n<form class=\"form\" [formGroup]=\"detailForm\">\n    <vdr-page-detail-layout>\n        <vdr-page-detail-sidebar>\n            <vdr-card *ngIf=\"entity$ | async as entity\">\n                <vdr-page-entity-info [entity]=\"entity\" />\n            </vdr-card>\n        </vdr-page-detail-sidebar>\n        <vdr-page-block>\n            <vdr-card>\n                <div class=\"form-grid\">\n                    <vdr-form-field [label]=\"'settings.email-address' | translate\" for=\"emailAddress\">\n                        <input id=\"emailAddress\" type=\"text\" formControlName=\"emailAddress\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'settings.first-name' | translate\" for=\"firstName\">\n                        <input id=\"firstName\" type=\"text\" formControlName=\"firstName\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'settings.last-name' | translate\" for=\"lastName\">\n                        <input id=\"lastName\" type=\"text\" formControlName=\"lastName\" />\n                    </vdr-form-field>\n                    <vdr-form-field\n                        *ngIf=\"isNew$ | async\"\n                        [label]=\"'settings.password' | translate\"\n                        for=\"password\"\n                    >\n                        <input id=\"password\" type=\"password\" formControlName=\"password\" />\n                    </vdr-form-field>\n                    <vdr-form-field\n                        [label]=\"'settings.password' | translate\"\n                        for=\"password\"\n                        [readOnlyToggle]=\"true\"\n                    >\n                        <input id=\"password\" type=\"password\" formControlName=\"password\" />\n                    </vdr-form-field>\n                </div>\n            </vdr-card>\n            <vdr-card\n                formGroupName=\"customFields\"\n                *ngIf=\"customFields.length\"\n                [title]=\"'common.custom-fields' | translate\"\n            >\n                <vdr-tabbed-custom-fields\n                    entityName=\"Administrator\"\n                    [customFields]=\"customFields\"\n                    [customFieldsFormGroup]=\"detailForm.get('customFields')\"\n                ></vdr-tabbed-custom-fields>\n            </vdr-card>\n            <vdr-custom-detail-component-host\n                locationId=\"administrator-profile\"\n                [entity$]=\"entity$\"\n                [detailForm]=\"detailForm\"\n            ></vdr-custom-detail-component-host>\n        </vdr-page-block>\n    </vdr-page-detail-layout>\n</form>\n" }]
        }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i1.DataService }, { type: i2.FormBuilder }, { type: i1.NotificationService }]; } });
//# sourceMappingURL=data:application/json;base64, 