import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Validators } from '@angular/forms';
import { concat, of, Subject } from 'rxjs';
import { catchError, debounceTime, distinctUntilChanged, switchMap, tap } 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 "@clr/angular";
import * as i4 from "@ng-select/ng-select";
import * as i5 from "@angular/common";
import * as i6 from "@ngx-translate/core";
export class SelectCustomerDialogComponent {
    constructor(dataService, formBuilder) {
        this.dataService = dataService;
        this.formBuilder = formBuilder;
        this.isLoading = false;
        this.input$ = new Subject();
        this.selectedCustomer = [];
        this.useExisting = true;
        this.createNew = false;
        this.customerForm = this.formBuilder.group({
            title: '',
            firstName: ['', Validators.required],
            lastName: ['', Validators.required],
            phoneNumber: '',
            emailAddress: ['', [Validators.required, Validators.email]],
        });
    }
    ngOnInit() {
        this.customers$ = concat(of([]), // default items
        this.input$.pipe(debounceTime(200), distinctUntilChanged(), tap(() => (this.isLoading = true)), switchMap(term => this.dataService.customer
            .getCustomerList(10, 0, term)
            .mapStream(({ customers }) => customers.items)
            .pipe(catchError(() => of([])), // empty list on error
        tap(() => (this.isLoading = false))))));
    }
    trackByFn(item) {
        return item.id;
    }
    cancel() {
        this.resolveWith();
    }
    select() {
        if (this.useExisting && this.selectedCustomer.length === 1) {
            this.resolveWith(this.selectedCustomer[0]);
        }
        if (this.createNew && this.customerForm.valid) {
            const formValue = this.customerForm.value;
            this.resolveWith(formValue);
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: SelectCustomerDialogComponent, deps: [{ token: i1.DataService }, { token: i2.UntypedFormBuilder }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.2", type: SelectCustomerDialogComponent, selector: "vdr-select-customer-dialog", ngImport: i0, template: "<ng-template vdrDialogTitle>{{ 'order.set-customer-for-order' | translate }}</ng-template>\n\n<clr-tabs>\n    <clr-tab>\n        <button clrTabLink>{{ 'order.existing-customer' | translate }}</button>\n\n        <ng-template [(clrIfActive)]=\"useExisting\">\n            <clr-tab-content>\n                <div class=\"mt-4\">\n                    <ng-select\n                        [items]=\"customers$ | async\"\n                        appendTo=\"body\"\n                        bindLabel=\"name\"\n                        [addTag]=\"false\"\n                        [multiple]=\"true\"\n                        [hideSelected]=\"true\"\n                        [trackByFn]=\"trackByFn\"\n                        [minTermLength]=\"2\"\n                        [loading]=\"isLoading\"\n                        [typeahead]=\"input$\"\n                        [(ngModel)]=\"selectedCustomer\"\n                    >\n                        <ng-template ng-label-tmp let-item=\"item\" let-clear=\"clear\">\n                            <span class=\"item-row\">\n                            <clr-icon shape=\"user\" class=\"is-solid\"></clr-icon\n                            ><span class=\"mx-1\">{{ item.firstName }} {{ item.lastName }}</span>\n                            <vdr-chip>{{ item.emailAddress }}</vdr-chip>\n                            </span>\n                        </ng-template>\n                        <ng-template ng-option-tmp let-item=\"item\">\n                            <span class=\"item-row\">\n                            <clr-icon shape=\"user\" class=\"is-solid\"></clr-icon\n                            ><span class=\"mx-1\">{{ item.firstName }} {{ item.lastName }}</span>\n                            <vdr-chip>{{ item.emailAddress }}</vdr-chip>\n                            </span>\n                        </ng-template>\n                    </ng-select>\n                </div>\n            </clr-tab-content>\n        </ng-template>\n    </clr-tab>\n    <clr-tab>\n        <button clrTabLink>{{ 'customer.create-new-customer' | translate }}</button>\n\n        <ng-template [(clrIfActive)]=\"createNew\">\n            <clr-tab-content>\n                <form [formGroup]=\"customerForm\">\n                    <vdr-form-field [label]=\"'customer.title' | translate\" for=\"title\">\n                        <input id=\"title\" type=\"text\" formControlName=\"title\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.first-name' | translate\" for=\"firstName\">\n                        <input id=\"firstName\" type=\"text\" formControlName=\"firstName\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.last-name' | translate\" for=\"lastName\">\n                        <input id=\"lastName\" type=\"text\" formControlName=\"lastName\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.email-address' | translate\" for=\"emailAddress\">\n                        <input id=\"emailAddress\" type=\"text\" formControlName=\"emailAddress\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.phone-number' | translate\" for=\"phoneNumber\">\n                        <input id=\"phoneNumber\" type=\"text\" formControlName=\"phoneNumber\" />\n                    </vdr-form-field>\n                </form>\n            </clr-tab-content>\n        </ng-template>\n    </clr-tab>\n</clr-tabs>\n\n<ng-template vdrDialogButtons>\n    <button type=\"button\" class=\"btn\" (click)=\"cancel()\">{{ 'common.cancel' | translate }}</button>\n    <button\n        type=\"submit\"\n        (click)=\"select()\"\n        [disabled]=\"(useExisting && selectedCustomer.length === 0) || (createNew && customerForm.invalid)\"\n        class=\"btn btn-primary\"\n    >\n        {{ 'common.okay' | translate }}\n    </button>\n</ng-template>\n", styles: [".item-row{display:flex;align-items:center}\n"], dependencies: [{ kind: "directive", type: i3.ClrIconCustomTag, selector: "clr-icon" }, { kind: "directive", type: i3.ClrIfActive, selector: "[clrIfActive]", inputs: ["clrIfActive"], outputs: ["clrIfActiveChange"] }, { kind: "component", type: i3.ClrTabContent, selector: "clr-tab-content", inputs: ["id"] }, { kind: "component", type: i3.ClrTab, selector: "clr-tab" }, { kind: "component", type: i3.ClrTabs, selector: "clr-tabs", inputs: ["clrLayout"] }, { kind: "directive", type: i3.ClrTabLink, selector: "[clrTabLink]", inputs: ["id", "clrTabLinkInOverflow"] }, { kind: "directive", type: i3.ÇlrTabsWillyWonka, selector: "clr-tabs" }, { kind: "directive", type: i3.ÇlrActiveOompaLoompa, selector: "[clrTabLink], clr-tab-content" }, { 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.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { 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: "component", type: i4.NgSelectComponent, selector: "ng-select", inputs: ["bindLabel", "bindValue", "markFirst", "placeholder", "notFoundText", "typeToSearchText", "addTagText", "loadingText", "clearAllText", "appearance", "dropdownPosition", "appendTo", "loading", "closeOnSelect", "hideSelected", "selectOnTab", "openOnEnter", "maxSelectedItems", "groupBy", "groupValue", "bufferAmount", "virtualScroll", "selectableGroup", "selectableGroupAsModel", "searchFn", "trackByFn", "clearOnBackspace", "labelForId", "inputAttrs", "tabIndex", "readonly", "searchWhileComposing", "minTermLength", "editableSearchTerm", "keyDownFn", "typeahead", "multiple", "addTag", "searchable", "clearable", "isOpen", "items", "compareWith", "clearSearchOnAdd", "deselectOnClick"], outputs: ["blur", "focus", "change", "open", "close", "search", "clear", "add", "remove", "scroll", "scrollToEnd"] }, { kind: "directive", type: i4.NgOptionTemplateDirective, selector: "[ng-option-tmp]" }, { kind: "directive", type: i4.NgLabelTemplateDirective, selector: "[ng-label-tmp]" }, { kind: "component", type: i1.ChipComponent, selector: "vdr-chip", inputs: ["icon", "invert", "colorFrom", "colorType"], outputs: ["iconClick"] }, { 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: "directive", type: i1.DialogButtonsDirective, selector: "[vdrDialogButtons]" }, { kind: "directive", type: i1.DialogTitleDirective, selector: "[vdrDialogTitle]" }, { kind: "pipe", type: i5.AsyncPipe, name: "async" }, { kind: "pipe", type: i6.TranslatePipe, name: "translate" }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: SelectCustomerDialogComponent, decorators: [{
            type: Component,
            args: [{ selector: 'vdr-select-customer-dialog', changeDetection: ChangeDetectionStrategy.OnPush, template: "<ng-template vdrDialogTitle>{{ 'order.set-customer-for-order' | translate }}</ng-template>\n\n<clr-tabs>\n    <clr-tab>\n        <button clrTabLink>{{ 'order.existing-customer' | translate }}</button>\n\n        <ng-template [(clrIfActive)]=\"useExisting\">\n            <clr-tab-content>\n                <div class=\"mt-4\">\n                    <ng-select\n                        [items]=\"customers$ | async\"\n                        appendTo=\"body\"\n                        bindLabel=\"name\"\n                        [addTag]=\"false\"\n                        [multiple]=\"true\"\n                        [hideSelected]=\"true\"\n                        [trackByFn]=\"trackByFn\"\n                        [minTermLength]=\"2\"\n                        [loading]=\"isLoading\"\n                        [typeahead]=\"input$\"\n                        [(ngModel)]=\"selectedCustomer\"\n                    >\n                        <ng-template ng-label-tmp let-item=\"item\" let-clear=\"clear\">\n                            <span class=\"item-row\">\n                            <clr-icon shape=\"user\" class=\"is-solid\"></clr-icon\n                            ><span class=\"mx-1\">{{ item.firstName }} {{ item.lastName }}</span>\n                            <vdr-chip>{{ item.emailAddress }}</vdr-chip>\n                            </span>\n                        </ng-template>\n                        <ng-template ng-option-tmp let-item=\"item\">\n                            <span class=\"item-row\">\n                            <clr-icon shape=\"user\" class=\"is-solid\"></clr-icon\n                            ><span class=\"mx-1\">{{ item.firstName }} {{ item.lastName }}</span>\n                            <vdr-chip>{{ item.emailAddress }}</vdr-chip>\n                            </span>\n                        </ng-template>\n                    </ng-select>\n                </div>\n            </clr-tab-content>\n        </ng-template>\n    </clr-tab>\n    <clr-tab>\n        <button clrTabLink>{{ 'customer.create-new-customer' | translate }}</button>\n\n        <ng-template [(clrIfActive)]=\"createNew\">\n            <clr-tab-content>\n                <form [formGroup]=\"customerForm\">\n                    <vdr-form-field [label]=\"'customer.title' | translate\" for=\"title\">\n                        <input id=\"title\" type=\"text\" formControlName=\"title\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.first-name' | translate\" for=\"firstName\">\n                        <input id=\"firstName\" type=\"text\" formControlName=\"firstName\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.last-name' | translate\" for=\"lastName\">\n                        <input id=\"lastName\" type=\"text\" formControlName=\"lastName\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.email-address' | translate\" for=\"emailAddress\">\n                        <input id=\"emailAddress\" type=\"text\" formControlName=\"emailAddress\" />\n                    </vdr-form-field>\n                    <vdr-form-field [label]=\"'customer.phone-number' | translate\" for=\"phoneNumber\">\n                        <input id=\"phoneNumber\" type=\"text\" formControlName=\"phoneNumber\" />\n                    </vdr-form-field>\n                </form>\n            </clr-tab-content>\n        </ng-template>\n    </clr-tab>\n</clr-tabs>\n\n<ng-template vdrDialogButtons>\n    <button type=\"button\" class=\"btn\" (click)=\"cancel()\">{{ 'common.cancel' | translate }}</button>\n    <button\n        type=\"submit\"\n        (click)=\"select()\"\n        [disabled]=\"(useExisting && selectedCustomer.length === 0) || (createNew && customerForm.invalid)\"\n        class=\"btn btn-primary\"\n    >\n        {{ 'common.okay' | translate }}\n    </button>\n</ng-template>\n", styles: [".item-row{display:flex;align-items:center}\n"] }]
        }], ctorParameters: function () { return [{ type: i1.DataService }, { type: i2.UntypedFormBuilder }]; } });
//# sourceMappingURL=data:application/json;base64, 