/**
 * @license
 * Copyright Endlessjs. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Inject, Injectable, LOCALE_ID } from '@angular/core';
import { DatePipe, FormStyle, getLocaleDayNames, getLocaleFirstDayOfWeek, getLocaleMonthNames, TranslationWidth, } from '@angular/common';
import { ElDateService } from './date.service';
/**
 * The `ElNativeDateService` is basic implementation of `ElDateService` using
 * native js date objects and angular localization services.
 * */
var ElNativeDateService = /** @class */ (function (_super) {
    __extends(ElNativeDateService, _super);
    function ElNativeDateService(locale) {
        var _this = _super.call(this) || this;
        _this.setLocale(locale);
        return _this;
    }
    ElNativeDateService.prototype.setLocale = function (locale) {
        _super.prototype.setLocale.call(this, locale);
        this.datePipe = new DatePipe(locale);
    };
    ElNativeDateService.prototype.isValidDateString = function (date, format) {
        return !isNaN(this.parse(date, format).getTime());
    };
    ElNativeDateService.prototype.today = function () {
        return new Date();
    };
    ElNativeDateService.prototype.getDate = function (date) {
        return date.getDate();
    };
    ElNativeDateService.prototype.getMonth = function (date) {
        return date.getMonth();
    };
    ElNativeDateService.prototype.getYear = function (date) {
        return date.getFullYear();
    };
    ElNativeDateService.prototype.getDayOfWeek = function (date) {
        return date.getDay();
    };
    /**
     * returns first day of the week, it can be 1 if week starts from monday
     * and 0 if from sunday and so on.
     * */
    ElNativeDateService.prototype.getFirstDayOfWeek = function () {
        return getLocaleFirstDayOfWeek(this.locale);
    };
    ElNativeDateService.prototype.getMonthName = function (date, style) {
        if (style === void 0) { style = TranslationWidth.Abbreviated; }
        var index = date.getMonth();
        return this.getMonthNameByIndex(index, style);
    };
    ElNativeDateService.prototype.getMonthNameByIndex = function (index, style) {
        if (style === void 0) { style = TranslationWidth.Abbreviated; }
        return getLocaleMonthNames(this.locale, FormStyle.Format, style)[index];
    };
    ElNativeDateService.prototype.getDayOfWeekNames = function () {
        return getLocaleDayNames(this.locale, FormStyle.Format, TranslationWidth.Short);
    };
    ElNativeDateService.prototype.format = function (date, format) {
        return this.datePipe.transform(date, format);
    };
    /**
     * We haven't got capability to parse date using formatting without third party libraries.
     * */
    ElNativeDateService.prototype.parse = function (date, format) {
        return new Date(Date.parse(date));
    };
    ElNativeDateService.prototype.addDay = function (date, num) {
        return this.createDate(date.getFullYear(), date.getMonth(), date.getDate() + num);
    };
    ElNativeDateService.prototype.addMonth = function (date, num) {
        var month = this.createDate(date.getFullYear(), date.getMonth() + num, 1);
        // In case of date has more days than calculated month js Date will change that month to the next one
        // because of the date overflow.
        month.setDate(Math.min(date.getDate(), this.getMonthEnd(month).getDate()));
        return month;
    };
    ElNativeDateService.prototype.addYear = function (date, num) {
        return this.createDate(date.getFullYear() + num, date.getMonth(), date.getDate());
    };
    ElNativeDateService.prototype.clone = function (date) {
        return new Date(date.getTime());
    };
    ElNativeDateService.prototype.compareDates = function (date1, date2) {
        return date1.getTime() - date2.getTime();
    };
    ElNativeDateService.prototype.createDate = function (year, month, date) {
        var result = new Date(year, month, date);
        // We need to correct for the fact that JS native Date treats years in range [0, 99] as
        // abbreviations for 19xx.
        if (year >= 0 && year < 100) {
            result.setFullYear(result.getFullYear() - 1900);
        }
        return result;
    };
    ElNativeDateService.prototype.getMonthEnd = function (date) {
        return this.createDate(date.getFullYear(), date.getMonth() + 1, 0);
    };
    ElNativeDateService.prototype.getMonthStart = function (date) {
        return this.createDate(date.getFullYear(), date.getMonth(), 1);
    };
    ElNativeDateService.prototype.getNumberOfDaysInMonth = function (date) {
        return this.getMonthEnd(date).getDate();
    };
    ElNativeDateService.prototype.getYearEnd = function (date) {
        return this.createDate(date.getFullYear(), 11, 31);
    };
    ElNativeDateService.prototype.getYearStart = function (date) {
        return this.createDate(date.getFullYear(), 0, 1);
    };
    ElNativeDateService.prototype.isSameDay = function (date1, date2) {
        return this.isSameMonth(date1, date2) &&
            date1.getDate() === date2.getDate();
    };
    ElNativeDateService.prototype.isSameMonth = function (date1, date2) {
        return this.isSameYear(date1, date2) &&
            date1.getMonth() === date2.getMonth();
    };
    ElNativeDateService.prototype.isSameYear = function (date1, date2) {
        return date1.getFullYear() === date2.getFullYear();
    };
    ElNativeDateService.prototype.getId = function () {
        return 'native';
    };
    ElNativeDateService.prototype.getWeekNumber = function (date) {
        return parseInt(this.datePipe.transform(date, 'w'), 10);
    };
    ElNativeDateService = __decorate([
        Injectable(),
        __param(0, Inject(LOCALE_ID)),
        __metadata("design:paramtypes", [String])
    ], ElNativeDateService);
    return ElNativeDateService;
}(ElDateService));
export { ElNativeDateService };
//# sourceMappingURL=native-date.service.js.map