import * as i0 from '@angular/core';
import { EventEmitter, Component, Output, Input, NgModule } from '@angular/core';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import { Subject, BehaviorSubject, map, takeUntil, filter, take } from 'rxjs';
import * as i1 from '@mintplayer/ng-bootstrap/calendar-month';
import { BsMonthNamePipeModule, BsWeekdayNameModule } from '@mintplayer/ng-bootstrap/calendar-month';
import * as i3 from '@mintplayer/ng-bootstrap/icon';
import { BsIconModule } from '@mintplayer/ng-bootstrap/icon';
import * as i4 from '@mintplayer/ng-bootstrap';
import { BsUcFirstPipeModule } from '@mintplayer/ng-bootstrap';

class BsCalendarComponent {
    constructor(calendarMonthService) {
        this.calendarMonthService = calendarMonthService;
        this.destroyed$ = new Subject();
        //#region CurrentMonth
        this.currentMonth$ = new BehaviorSubject(new Date());
        this.currentMonthChange = new EventEmitter();
        //#endregion
        //#region SelectedDate
        this.selectedDate$ = new BehaviorSubject(new Date());
        this.selectedDateChange = new EventEmitter();
        this.weeks$ = this.currentMonth$
            .pipe(map((month) => this.calendarMonthService.getWeeks(month)))
            .pipe(takeUntil(this.destroyed$));
        this.shownDays$ = this.weeks$
            .pipe(filter((weeks) => weeks.length > 1))
            .pipe(map((weeks) => weeks[1].days))
            .pipe(map((days) => {
            const firstDay = days[0];
            if (firstDay) {
                return days.map((d) => {
                    const date = new Date(firstDay.date.getFullYear(), firstDay.date.getMonth(), d?.dayOfMonth);
                    return {
                        short: date.toLocaleString('default', { weekday: 'short' }),
                        long: date.toLocaleString('default', { weekday: 'long' })
                    };
                });
            }
            else {
                return [];
            }
        }));
        this.selectedDate$.pipe(takeUntil(this.destroyed$)).subscribe((date) => {
            this.selectedDateChange.emit(date);
        });
        this.currentMonth$.pipe(takeUntil(this.destroyed$)).subscribe((month) => {
            this.currentMonthChange.emit(month);
        });
    }
    get currentMonth() {
        return this.currentMonth$.value;
    }
    set currentMonth(value) {
        this.currentMonth$.next(value);
    }
    get selectedDate() {
        return this.selectedDate$.value;
    }
    set selectedDate(value) {
        this.selectedDate$.next(value);
    }
    //#endregion
    ngOnDestroy() {
        this.destroyed$.next(true);
    }
    previousMonth() {
        this.currentMonth$.pipe(take(1)).subscribe((month) => {
            this.currentMonth$.next(new Date(month.getFullYear(), month.getMonth() - 1, 1));
        });
        return false;
    }
    nextMonth() {
        this.currentMonth$.pipe(take(1)).subscribe((month) => {
            this.currentMonth$.next(new Date(month.getFullYear(), month.getMonth() + 1, 1));
        });
        return false;
    }
    isSameDate(date1, date2) {
        if (date1 === null && date2 === null)
            return true;
        if (date1 === null || date2 === null)
            return false;
        return (date1.getFullYear() === date2.getFullYear() &&
            date1.getMonth() === date2.getMonth() &&
            date1.getDate() === date2.getDate());
    }
    goto(day) {
        if (day) {
            this.selectedDate$.next(day.date);
        }
    }
}
BsCalendarComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarComponent, deps: [{ token: i1.BsCalendarMonthService }], target: i0.ɵɵFactoryTarget.Component });
BsCalendarComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.0.4", type: BsCalendarComponent, selector: "bs-calendar", inputs: { currentMonth: "currentMonth", selectedDate: "selectedDate" }, outputs: { currentMonthChange: "currentMonthChange", selectedDateChange: "selectedDateChange" }, ngImport: i0, template: "<table class=\"table w-auto mb-0\">\n    <tr>\n        <td>\n            <button class=\"btn btn-default\" (click)=\"previousMonth()\">\n                <bs-icon [icon]=\"'chevron-left'\" class=\"fw-bolder\"></bs-icon>\n            </button>\n        </td>\n        <td colspan=\"6\" class=\"fw-bolder\">\n            {{ currentMonth$ | async | monthName | ucFirst }}\n            {{ (currentMonth$ | async)?.getFullYear() }}\n        </td>\n        <td>\n            <button class=\"btn btn-default border-bottom-0\" (click)=\"nextMonth()\">\n                <bs-icon [icon]=\"'chevron-right'\" class=\"fw-bolder\"></bs-icon>\n            </button>\n        </td>\n    </tr>\n    <tr>\n        <th></th>\n        <th *ngFor=\"let dayOfWeek of shownDays$ | async\" [title]=\"dayOfWeek.long\">\n            {{ dayOfWeek.short }}\n        </th>\n    </tr>\n    <tr *ngFor=\"let week of (weeks$ | async)\">\n        <th>\n            {{ week.number }}\n        </th>\n        <td [class.selected]=\"day === null ? false : isSameDate(selectedDate$ | async, day.date)\" *ngFor=\"let day of week.days\" (click)=\"goto(day)\">\n            <span *ngIf=\"day !== null\" [class.text-muted]=\"!day.isInMonth\">{{ day.dayOfMonth }}</span>\n        </td>\n    </tr>\n</table>", styles: [".text-muted{color:#9ea7af!important}table{border-collapse:collapse;border:1px solid rgba(0,0,0,.125);background:#fff}table td,table th{width:40px;height:40px;text-align:center}table td span,table th span{cursor:pointer;white-space:nowrap}table td.selected{background-color:#0d6efd;color:#fff}table th{background-color:#f8f9fa}table tr{border-top:none}table tr:first-child>td{border-bottom:1px solid rgba(0,0,0,.125)}table tr:nth-of-type(2)>th:not(:nth-of-type(1)){border-bottom:1px solid rgba(0,0,0,.125)}table tr:not(:nth-of-type(2))>th:nth-of-type(1){border-right:1px solid rgba(0,0,0,.125)}\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: "component", type: i3.BsIconComponent, selector: "bs-icon", inputs: ["icon"] }, { kind: "pipe", type: i2.AsyncPipe, name: "async" }, { kind: "pipe", type: i4.UcFirstPipe, name: "ucFirst" }, { kind: "pipe", type: i1.MonthNamePipe, name: "monthName" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarComponent, decorators: [{
            type: Component,
            args: [{ selector: 'bs-calendar', template: "<table class=\"table w-auto mb-0\">\n    <tr>\n        <td>\n            <button class=\"btn btn-default\" (click)=\"previousMonth()\">\n                <bs-icon [icon]=\"'chevron-left'\" class=\"fw-bolder\"></bs-icon>\n            </button>\n        </td>\n        <td colspan=\"6\" class=\"fw-bolder\">\n            {{ currentMonth$ | async | monthName | ucFirst }}\n            {{ (currentMonth$ | async)?.getFullYear() }}\n        </td>\n        <td>\n            <button class=\"btn btn-default border-bottom-0\" (click)=\"nextMonth()\">\n                <bs-icon [icon]=\"'chevron-right'\" class=\"fw-bolder\"></bs-icon>\n            </button>\n        </td>\n    </tr>\n    <tr>\n        <th></th>\n        <th *ngFor=\"let dayOfWeek of shownDays$ | async\" [title]=\"dayOfWeek.long\">\n            {{ dayOfWeek.short }}\n        </th>\n    </tr>\n    <tr *ngFor=\"let week of (weeks$ | async)\">\n        <th>\n            {{ week.number }}\n        </th>\n        <td [class.selected]=\"day === null ? false : isSameDate(selectedDate$ | async, day.date)\" *ngFor=\"let day of week.days\" (click)=\"goto(day)\">\n            <span *ngIf=\"day !== null\" [class.text-muted]=\"!day.isInMonth\">{{ day.dayOfMonth }}</span>\n        </td>\n    </tr>\n</table>", styles: [".text-muted{color:#9ea7af!important}table{border-collapse:collapse;border:1px solid rgba(0,0,0,.125);background:#fff}table td,table th{width:40px;height:40px;text-align:center}table td span,table th span{cursor:pointer;white-space:nowrap}table td.selected{background-color:#0d6efd;color:#fff}table th{background-color:#f8f9fa}table tr{border-top:none}table tr:first-child>td{border-bottom:1px solid rgba(0,0,0,.125)}table tr:nth-of-type(2)>th:not(:nth-of-type(1)){border-bottom:1px solid rgba(0,0,0,.125)}table tr:not(:nth-of-type(2))>th:nth-of-type(1){border-right:1px solid rgba(0,0,0,.125)}\n"] }]
        }], ctorParameters: function () { return [{ type: i1.BsCalendarMonthService }]; }, propDecorators: { currentMonthChange: [{
                type: Output
            }], currentMonth: [{
                type: Input
            }], selectedDateChange: [{
                type: Output
            }], selectedDate: [{
                type: Input
            }] } });

class BsCalendarModule {
}
BsCalendarModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
BsCalendarModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarModule, declarations: [BsCalendarComponent], imports: [CommonModule,
        BsIconModule,
        BsUcFirstPipeModule,
        BsMonthNamePipeModule,
        BsWeekdayNameModule], exports: [BsCalendarComponent] });
BsCalendarModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarModule, imports: [CommonModule,
        BsIconModule,
        BsUcFirstPipeModule,
        BsMonthNamePipeModule,
        BsWeekdayNameModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        BsCalendarComponent
                    ],
                    imports: [
                        CommonModule,
                        BsIconModule,
                        BsUcFirstPipeModule,
                        BsMonthNamePipeModule,
                        BsWeekdayNameModule
                    ],
                    exports: [
                        BsCalendarComponent
                    ]
                }]
        }] });

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

export { BsCalendarComponent, BsCalendarModule };
//# sourceMappingURL=mintplayer-ng-bootstrap-calendar.mjs.map
