import { __decorate, __metadata } from 'tslib';
import * as i0 from '@angular/core';
import { EventEmitter, HostBinding, Output, Input, Component, NgModule } from '@angular/core';
import * as i1 from '@aztrix/sdk';
import * as i2 from 'angular-container-media-query';
import { MediaQuery } from 'angular-container-media-query';
import { BehaviorSubject, ReplaySubject, distinctUntilChanged, switchMap, shareReplay, Subscription, first } from 'rxjs';
import * as i3 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i4 from '@aztrix/components/stripe-checkout';
import { StripeCheckoutModule } from '@aztrix/components/stripe-checkout';
import * as i5 from '@aztrix/components/spinner';
import { SpinnerModule } from '@aztrix/components/spinner';
import { ReactiveFormsModule } from '@angular/forms';
import { MatStepperModule } from '@angular/material/stepper';
import { ButtonModule } from '@aztrix/components/button';
import { PropertyEditModule } from '@aztrix/components/property-edit';
import { TranslatePipe } from '@aztrix/translate';
import { IconsModule } from '@aztrix/icons';

class ConfirmPaymentComponent {
    constructor(_productService, resize, elementRef, changeDetector) {
        this._productService = _productService;
        this.done = new EventEmitter();
        this.medium = false;
        this.paymentBusy$ = new BehaviorSubject(false);
        this.stripeInvoice$ = new ReplaySubject(1);
        this.productId$ = new ReplaySubject(1);
        this.paymentIntent$ = new ReplaySubject(1);
        this.product$ = this.productId$.pipe(distinctUntilChanged(), switchMap((productId) => this._productService.getProductById(this.ownerId, productId)), shareReplay(1));
        this._subscriptions = new Subscription();
        resize.register(this, elementRef, changeDetector);
    }
    ngOnChanges(changes) {
        if (changes.productId && this.productId) {
            this.productId$.next(this.productId);
        }
    }
    ngOnDestroy() {
        this._subscriptions.unsubscribe();
    }
    setPaymentIntent() {
        this._productService
            .getPaymentIntentForProduct(this.ownerId, this.productId)
            .pipe(first())
            .subscribe((paymentIntent) => {
            this.paymentIntent$.next(paymentIntent);
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ConfirmPaymentComponent, deps: [{ token: i1.ProductsService }, { token: i2.ObserveResizeService }, { token: i0.ElementRef }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: ConfirmPaymentComponent, selector: "ax-confirm-payment", inputs: { productId: "productId", ownerId: "ownerId" }, outputs: { done: "done" }, host: { properties: { "class.medium": "this.medium" } }, usesOnChanges: true, ngImport: i0, template: "<ax-spinner *ngIf=\"(product$ | async) === null; else content\"></ax-spinner>\n\n<ng-template #content>\n  <ng-container *ngIf=\"product$ | async as product\">\n    <div class=\"product-info\">\n      <h4>{{ product.name }}</h4>\n      <h2 class=\"price\">\n        {{ (product.price?.amount || 0) / 100 | currency: product.price?.currency?.toUpperCase() }}\n      </h2>\n      <p>{{ product.description }}</p>\n    </div>\n  </ng-container>\n\n  <ax-spinner *ngIf=\"(paymentIntent$ | async) === null; else checkout\"></ax-spinner>\n  <ng-template #checkout>\n    <ax-stripe-checkout\n      [clientSecret]=\"(paymentIntent$ | async)?.clientSecret\"\n      [accountId]=\"(paymentIntent$ | async)?.stripeAccount\"\n      (cancel)=\"done.next()\"\n      (payed)=\"done.next()\"\n    ></ax-stripe-checkout>\n  </ng-template>\n</ng-template>\n", styles: [":host{display:block;padding:1rem 1rem .5rem}:host.medium{text-align:inherit}.product-info{text-align:center;margin-bottom:2rem}.stripe-card{margin-bottom:1rem}\n"], dependencies: [{ kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i4.StripeCheckoutComponent, selector: "ax-stripe-checkout", inputs: ["clientSecret", "accountId"], outputs: ["cancel", "payed"] }, { kind: "component", type: i5.SpinnerComponent, selector: "ax-spinner" }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }, { kind: "pipe", type: i3.CurrencyPipe, name: "currency" }] }); }
}
__decorate([
    MediaQuery('(min-width: 37.5rem)'),
    __metadata("design:type", Object)
], ConfirmPaymentComponent.prototype, "medium", void 0);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ConfirmPaymentComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-confirm-payment', template: "<ax-spinner *ngIf=\"(product$ | async) === null; else content\"></ax-spinner>\n\n<ng-template #content>\n  <ng-container *ngIf=\"product$ | async as product\">\n    <div class=\"product-info\">\n      <h4>{{ product.name }}</h4>\n      <h2 class=\"price\">\n        {{ (product.price?.amount || 0) / 100 | currency: product.price?.currency?.toUpperCase() }}\n      </h2>\n      <p>{{ product.description }}</p>\n    </div>\n  </ng-container>\n\n  <ax-spinner *ngIf=\"(paymentIntent$ | async) === null; else checkout\"></ax-spinner>\n  <ng-template #checkout>\n    <ax-stripe-checkout\n      [clientSecret]=\"(paymentIntent$ | async)?.clientSecret\"\n      [accountId]=\"(paymentIntent$ | async)?.stripeAccount\"\n      (cancel)=\"done.next()\"\n      (payed)=\"done.next()\"\n    ></ax-stripe-checkout>\n  </ng-template>\n</ng-template>\n", styles: [":host{display:block;padding:1rem 1rem .5rem}:host.medium{text-align:inherit}.product-info{text-align:center;margin-bottom:2rem}.stripe-card{margin-bottom:1rem}\n"] }]
        }], ctorParameters: () => [{ type: i1.ProductsService }, { type: i2.ObserveResizeService }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }], propDecorators: { productId: [{
                type: Input
            }], ownerId: [{
                type: Input
            }], done: [{
                type: Output
            }], medium: [{
                type: HostBinding,
                args: ['class.medium']
            }] } });

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

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

export { ConfirmPaymentComponent, ConfirmPaymentModule };
//# sourceMappingURL=aztrix-components-confirm-payment.mjs.map
