import { __decorate } from "tslib";
import { Component } from '@angular/core';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import * as i0 from "@angular/core";
import * as i1 from "../../core/http.service";
import * as i2 from "@angular/common";
import * as i3 from "ngx-infinite-scroll";
import * as i4 from "@angular/material/button";
import * as i5 from "@angular/material/icon";
import * as i6 from "@angular/material/core";
import * as i7 from "@angular/material/divider";
import * as i8 from "@angular/material/list";
import * as i9 from "@angular/flex-layout/flex";
let EmailSubscriptionForUserComponent = class EmailSubscriptionForUserComponent {
    constructor(http) {
        this.http = http;
        this.subscriptions = [];
        this.pageEvent = { page: 1, perPage: 25, lastPage: 1 };
        this.filters = {
            sort_by: 'created_at|desc',
            isLoadRelations: true,
            relations: 'frequency,currentUserSubscription'
        };
    }
    ngOnInit() {
        this.fetchSubscriptions();
    }
    fetchSubscriptions() {
        this.http.get(`api/email-subscription-types`, { params: { ...this.filters, ...this.pageEvent } })
            .pipe(untilDestroyed(this))
            .subscribe((result) => {
            this.subscriptions = this.subscriptions.concat(result.data);
            this.pageEvent.page = result.current_page;
            this.pageEvent.lastPage = result.last_page;
        });
    }
    onScroll() {
        if (this.pageEvent.lastPage > this.pageEvent.page) {
            this.pageEvent.page += 1;
            this.fetchSubscriptions();
        }
    }
    subscribe(sub) {
        this.http.post(`api/email-subscription-for-user`, { email_type_id: sub?.id })
            .pipe(untilDestroyed(this))
            .subscribe((result) => {
            sub.current_user_subscription = result?.item;
        });
    }
    unsubscribe(sub) {
        this.http.delete(`api/email-subscription-users/${sub?.current_user_subscription?.id}`)
            .pipe(untilDestroyed(this))
            .subscribe((_) => {
            sub.current_user_subscription = null;
        });
    }
    reload() {
        this.subscriptions = [];
        this.pageEvent = { page: 1, perPage: 25, lastPage: 1 };
        this.fetchSubscriptions();
    }
    ngOnDestroy() {
        //Until destroyed uses it
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: EmailSubscriptionForUserComponent, deps: [{ token: i1.HttpService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: EmailSubscriptionForUserComponent, selector: "app-email-subscription-for-user", ngImport: i0, template: "<div fxLayout=\"column\" class=\"container my-4\">\n\n    <div class=\"scroll-container\" infiniteScroll [infiniteScrollDistance]=\"2\" [infiniteScrollThrottle]=\"50\"\n        (scrolled)=\"onScroll()\" [scrollWindow]=\"false\">\n        <mat-list>\n            <div mat-subheader>Available Subscriptions</div>\n            <mat-list-item *ngFor=\"let sub of subscriptions\">\n                <mat-icon mat-list-icon>email</mat-icon>\n                <div mat-line>{{sub?.title}} </div>\n                <div mat-line class=\"text-secondary\">{{sub?.frequency?.title}}</div>\n                <div mat-line> {{sub?.description}} </div>\n                <div fxLayout fxLayoutAlign=\"end\">\n                    <button type=\"button\" *ngIf=\"!sub?.current_user_subscription\" mat-raised-button color=\"primary\"\n                        (click)=\"subscribe(sub)\">Subscribe</button>\n                    <button type=\"button\" *ngIf=\"sub?.current_user_subscription\" mat-stroked-button color=\"primary\"\n                        (click)=\"unsubscribe(sub)\">Unsubscribe</button>\n                </div>\n                <mat-divider></mat-divider>\n            </mat-list-item>\n        </mat-list>\n    </div>\n\n</div>", styles: [".scroll-container{max-height:800px;overflow-y:auto}\n"], dependencies: [{ kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.InfiniteScrollDirective, selector: "[infiniteScroll], [infinite-scroll], [data-infinite-scroll]", inputs: ["infiniteScrollDistance", "infiniteScrollUpDistance", "infiniteScrollThrottle", "infiniteScrollDisabled", "infiniteScrollContainer", "scrollWindow", "immediateCheck", "horizontal", "alwaysCallback", "fromRoot"], outputs: ["scrolled", "scrolledUp"] }, { kind: "component", type: i4.MatButton, selector: "    button[mat-button], button[mat-raised-button], button[mat-flat-button],    button[mat-stroked-button]  ", exportAs: ["matButton"] }, { kind: "component", type: i5.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "directive", type: i6.MatLine, selector: "[mat-line], [matLine]" }, { kind: "component", type: i7.MatDivider, selector: "mat-divider", inputs: ["vertical", "inset"] }, { kind: "component", type: i8.MatList, selector: "mat-list", exportAs: ["matList"] }, { kind: "component", type: i8.MatListItem, selector: "mat-list-item, a[mat-list-item], button[mat-list-item]", inputs: ["activated"], exportAs: ["matListItem"] }, { kind: "directive", type: i8.MatListSubheaderCssMatStyler, selector: "[mat-subheader], [matSubheader]" }, { kind: "directive", type: i9.DefaultLayoutDirective, selector: "  [fxLayout], [fxLayout.xs], [fxLayout.sm], [fxLayout.md],  [fxLayout.lg], [fxLayout.xl], [fxLayout.lt-sm], [fxLayout.lt-md],  [fxLayout.lt-lg], [fxLayout.lt-xl], [fxLayout.gt-xs], [fxLayout.gt-sm],  [fxLayout.gt-md], [fxLayout.gt-lg]", inputs: ["fxLayout", "fxLayout.xs", "fxLayout.sm", "fxLayout.md", "fxLayout.lg", "fxLayout.xl", "fxLayout.lt-sm", "fxLayout.lt-md", "fxLayout.lt-lg", "fxLayout.lt-xl", "fxLayout.gt-xs", "fxLayout.gt-sm", "fxLayout.gt-md", "fxLayout.gt-lg"] }, { kind: "directive", type: i9.DefaultLayoutAlignDirective, selector: "  [fxLayoutAlign], [fxLayoutAlign.xs], [fxLayoutAlign.sm], [fxLayoutAlign.md],  [fxLayoutAlign.lg], [fxLayoutAlign.xl], [fxLayoutAlign.lt-sm], [fxLayoutAlign.lt-md],  [fxLayoutAlign.lt-lg], [fxLayoutAlign.lt-xl], [fxLayoutAlign.gt-xs], [fxLayoutAlign.gt-sm],  [fxLayoutAlign.gt-md], [fxLayoutAlign.gt-lg]", inputs: ["fxLayoutAlign", "fxLayoutAlign.xs", "fxLayoutAlign.sm", "fxLayoutAlign.md", "fxLayoutAlign.lg", "fxLayoutAlign.xl", "fxLayoutAlign.lt-sm", "fxLayoutAlign.lt-md", "fxLayoutAlign.lt-lg", "fxLayoutAlign.lt-xl", "fxLayoutAlign.gt-xs", "fxLayoutAlign.gt-sm", "fxLayoutAlign.gt-md", "fxLayoutAlign.gt-lg"] }] }); }
};
EmailSubscriptionForUserComponent = __decorate([
    UntilDestroy()
], EmailSubscriptionForUserComponent);
export { EmailSubscriptionForUserComponent };
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: EmailSubscriptionForUserComponent, decorators: [{
            type: Component,
            args: [{ selector: 'app-email-subscription-for-user', template: "<div fxLayout=\"column\" class=\"container my-4\">\n\n    <div class=\"scroll-container\" infiniteScroll [infiniteScrollDistance]=\"2\" [infiniteScrollThrottle]=\"50\"\n        (scrolled)=\"onScroll()\" [scrollWindow]=\"false\">\n        <mat-list>\n            <div mat-subheader>Available Subscriptions</div>\n            <mat-list-item *ngFor=\"let sub of subscriptions\">\n                <mat-icon mat-list-icon>email</mat-icon>\n                <div mat-line>{{sub?.title}} </div>\n                <div mat-line class=\"text-secondary\">{{sub?.frequency?.title}}</div>\n                <div mat-line> {{sub?.description}} </div>\n                <div fxLayout fxLayoutAlign=\"end\">\n                    <button type=\"button\" *ngIf=\"!sub?.current_user_subscription\" mat-raised-button color=\"primary\"\n                        (click)=\"subscribe(sub)\">Subscribe</button>\n                    <button type=\"button\" *ngIf=\"sub?.current_user_subscription\" mat-stroked-button color=\"primary\"\n                        (click)=\"unsubscribe(sub)\">Unsubscribe</button>\n                </div>\n                <mat-divider></mat-divider>\n            </mat-list-item>\n        </mat-list>\n    </div>\n\n</div>", styles: [".scroll-container{max-height:800px;overflow-y:auto}\n"] }]
        }], ctorParameters: () => [{ type: i1.HttpService }] });
//# sourceMappingURL=data:application/json;base64, 