import * as i0 from '@angular/core';
import { EventEmitter, ViewChild, Output, Input, Optional, Inject, Component, NgModule } from '@angular/core';
import * as i1 from '@aztrix/environment';
import { localeToString } from '@aztrix/helpers';
import * as i5 from '@aztrix/translate';
import { LOCALE, TranslatePipe } from '@aztrix/translate';
import * as i6 from 'rxjs';
import { BehaviorSubject, first } from 'rxjs';
import * as i2 from '@aztrix/components/button';
import { ButtonModule } from '@aztrix/components/button';
import * as i3 from '@aztrix/components/snackbar';
import { SnackbarModule } from '@aztrix/components/snackbar';
import * as i4 from '@angular/common';
import { CommonModule } from '@angular/common';
import { SpinnerModule } from '@aztrix/components/spinner';

class StripeCheckoutComponent {
    constructor(_environment, _locale$) {
        this._environment = _environment;
        this._locale$ = _locale$;
        this.cancel = new EventEmitter();
        this.payed = new EventEmitter();
        this._afterViewInit = false;
        this.loading$ = new BehaviorSubject(false);
    }
    ngOnInit() {
        this._environment
            .string$('STRIPE_API_KEY')
            .pipe(first((value) => !!value))
            .subscribe((stripeApiKey) => {
            this._stripeApiKey = stripeApiKey;
        });
    }
    ngOnChanges(changes) {
        if (changes.paymentIntent) {
            this._updateElements();
        }
    }
    _updateElements() {
        if (!this._stripeApiKey || !this.clientSecret || !this._afterViewInit) {
            return;
        }
        this._stripe = Stripe(this._stripeApiKey, this.accountId
            ? {
                stripeAccount: this.accountId,
            }
            : undefined);
        this._elements = this._stripe.elements({
            clientSecret: this.clientSecret,
            locale: this._locale$?.value ? localeToString(this._locale$.value) : undefined,
        });
        const checkout = this._elements.create('payment', {
            layout: {
                type: 'accordion',
                defaultCollapsed: false,
                radios: true,
                spacedAccordionItems: false,
            },
        });
        checkout.mount(this._checkoutElement.nativeElement);
    }
    ngAfterViewInit() {
        this._afterViewInit = true;
        this._updateElements();
    }
    async confirmPayment() {
        this.loading$.next(true);
        this.error = undefined;
        const { error } = await this._stripe.confirmPayment({
            elements: this._elements,
            confirmParams: {
                return_url: window.location.href,
            },
        });
        this.loading$.next(false);
        if (error) {
            this.error = error.message;
        }
        else {
            this.payed.next();
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: StripeCheckoutComponent, deps: [{ token: i1.EnvironmentService }, { token: LOCALE, optional: true }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: StripeCheckoutComponent, selector: "ax-stripe-checkout", inputs: { clientSecret: "clientSecret", accountId: "accountId" }, outputs: { cancel: "cancel", payed: "payed" }, viewQueries: [{ propertyName: "_checkoutElement", first: true, predicate: ["checkoutElement"], descendants: true }], usesOnChanges: true, ngImport: i0, template: "<div class=\"container\">\n  <div #checkoutElement></div>\n\n  <div class=\"h-button-wrapper\">\n    <ax-button (click)=\"cancel.next()\" [loading]=\"loading$ | async\">{{\n      'label.cancel' | translate\n    }}</ax-button>\n    <ax-button filled color=\"primary\" (click)=\"confirmPayment()\" [loading]=\"loading$ | async\">{{\n      'label.pay' | translate\n    }}</ax-button>\n  </div>\n\n  <ax-snackbar [open]=\"!!error\" color=\"red\" (didClose)=\"error = undefined\">{{ error }}</ax-snackbar>\n</div>\n", styles: [".StripeElement{padding-bottom:.5rem}\n"], dependencies: [{ kind: "component", type: i2.ButtonComponent, selector: "ax-button", inputs: ["url", "ariaLabel", "target"] }, { kind: "component", type: i3.SnackbarComponent, selector: "ax-snackbar", inputs: ["type", "timeout", "dismissible", "open", "icon", "color"], outputs: ["didClose"] }, { kind: "pipe", type: i4.AsyncPipe, name: "async" }, { kind: "pipe", type: i5.TranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: StripeCheckoutComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-stripe-checkout', template: "<div class=\"container\">\n  <div #checkoutElement></div>\n\n  <div class=\"h-button-wrapper\">\n    <ax-button (click)=\"cancel.next()\" [loading]=\"loading$ | async\">{{\n      'label.cancel' | translate\n    }}</ax-button>\n    <ax-button filled color=\"primary\" (click)=\"confirmPayment()\" [loading]=\"loading$ | async\">{{\n      'label.pay' | translate\n    }}</ax-button>\n  </div>\n\n  <ax-snackbar [open]=\"!!error\" color=\"red\" (didClose)=\"error = undefined\">{{ error }}</ax-snackbar>\n</div>\n", styles: [".StripeElement{padding-bottom:.5rem}\n"] }]
        }], ctorParameters: () => [{ type: i1.EnvironmentService }, { type: i6.BehaviorSubject, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [LOCALE]
                }] }], propDecorators: { clientSecret: [{
                type: Input
            }], accountId: [{
                type: Input
            }], cancel: [{
                type: Output
            }], payed: [{
                type: Output
            }], _checkoutElement: [{
                type: ViewChild,
                args: ['checkoutElement']
            }] } });

class StripeCheckoutModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: StripeCheckoutModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: StripeCheckoutModule, declarations: [StripeCheckoutComponent], imports: [CommonModule,
            ButtonModule,
            TranslatePipe,
            SpinnerModule,
            SnackbarModule], exports: [StripeCheckoutComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: StripeCheckoutModule, imports: [CommonModule,
            ButtonModule,
            SpinnerModule,
            SnackbarModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: StripeCheckoutModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [StripeCheckoutComponent],
                    imports: [
                        CommonModule,
                        ButtonModule,
                        TranslatePipe,
                        SpinnerModule,
                        SnackbarModule,
                        // MatSelectModule,
                        // PaymentMethodModule,
                    ],
                    exports: [StripeCheckoutComponent],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { StripeCheckoutComponent, StripeCheckoutModule };
//# sourceMappingURL=aztrix-components-stripe-checkout.mjs.map
