import * as i0 from '@angular/core';
import { Injectable, Pipe, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

class BsCalendarMonthService {
    getWeeks(month) {
        const firstAndLast = this.getFirstAndLastDayOfMonth(month);
        const firstOfMonth = this.getMondayBefore(firstAndLast.first);
        const lastOfMonth = this.getSundayAfter(firstAndLast.last);
        const days = this.dateDiff(firstOfMonth, lastOfMonth) + 1;
        const firstDayOffset = (firstAndLast.first.getDay() + 7 - 1) % 7;
        const allDays = this.generateList(days).map(d => d - firstDayOffset).map(d => {
            const wrongDate = new Date(firstAndLast.first.getFullYear(), firstAndLast.first.getMonth(), firstAndLast.first.getDate() + d);
            const correctDate = new Date(wrongDate.getFullYear(), wrongDate.getMonth(), wrongDate.getDate());
            return {
                date: correctDate,
                dayOfMonth: correctDate.getDate(),
                isInMonth: (correctDate.getFullYear() === month.getFullYear()) && (correctDate.getMonth() === month.getMonth())
            };
        });
        const weeks = this.chunk(allDays, 7);
        const weeksMapped = weeks.map((w, i) => ({
            number: this.weekOfYear(new Date(month.getFullYear(), month.getMonth(), w.find(d => d.isInMonth)?.dayOfMonth)),
            days: w
        }));
        return weeksMapped;
    }
    generateList(count) {
        return [...Array(count).keys()];
    }
    dayOfWeekMondayBased(date) {
        const d = date.getDay() - 1;
        if (d < 0) {
            return 6;
        }
        else {
            return d;
        }
    }
    weekOfYear(date) {
        const dateClone = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
        dateClone.setUTCDate(dateClone.getUTCDate() + 4 - (dateClone.getUTCDay() || 7));
        const yearStart = new Date(Date.UTC(dateClone.getUTCFullYear(), 0, 1));
        const utcDiff = this.toUTC(dateClone) - this.toUTC(yearStart);
        return Math.ceil((utcDiff / (1000 * 60 * 60 * 24) + 1) / 7);
    }
    getFirstAndLastDayOfMonth(date) {
        const first = new Date(date.getFullYear(), date.getMonth(), 1);
        const last = new Date(date.getFullYear(), date.getMonth() + 1, 0);
        return { first, last };
    }
    dateDiff(date1, date2) {
        const utc1 = this.toUTC(date1);
        const utc2 = this.toUTC(date2);
        const msPerDay = 1000 * 60 * 60 * 24;
        return Math.floor((utc2 - utc1) / msPerDay);
    }
    dayOfYear(date) {
        const start = new Date(date.getFullYear(), 0, 0);
        return this.dateDiff(start, date);
    }
    toUTC(date) {
        return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
    }
    chunk(items, size) {
        const result = [];
        for (let i = 0; i < items.length; i += size) {
            result.push(items.slice(i, i + size));
        }
        return result;
    }
    getMondayBefore(date) {
        const day = date.getDay(), diff = date.getDate() - day + (day == 0 ? -6 : 1); // adjust when day is sunday
        const monday = new Date(date);
        monday.setDate(diff);
        return monday;
    }
    getSundayAfter(date) {
        const day = date.getDay();
        const sunday = new Date(date);
        if (day !== 0) {
            const diff = date.getDate() + 7 - day;
            sunday.setDate(diff);
        }
        return sunday;
    }
}
BsCalendarMonthService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarMonthService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
BsCalendarMonthService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarMonthService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsCalendarMonthService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }] });

class MonthNamePipe {
    transform(date, ...args) {
        if (date) {
            return date.toLocaleString("default", { month: 'long' });
        }
        else {
            return '';
        }
    }
}
MonthNamePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: MonthNamePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
MonthNamePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "15.0.4", ngImport: i0, type: MonthNamePipe, name: "monthName" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: MonthNamePipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'monthName'
                }]
        }] });

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

class WeekdayNamePipe {
    transform(date, ...args) {
        return date.toLocaleString("default", { weekday: 'short' });
    }
}
WeekdayNamePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: WeekdayNamePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
WeekdayNamePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "15.0.4", ngImport: i0, type: WeekdayNamePipe, name: "weekdayName" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: WeekdayNamePipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'weekdayName'
                }]
        }] });

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

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

export { BsCalendarMonthService, BsMonthNamePipeModule, BsWeekdayNameModule, MonthNamePipe, WeekdayNamePipe };
//# sourceMappingURL=mintplayer-ng-bootstrap-calendar-month.mjs.map
