/*
 * Copyright 2015-2024 Ritense BV, the Netherlands.
 *
 * Licensed under EUPL, Version 1.2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Component, EventEmitter, HostBinding, Input, Output, ViewChild, } from '@angular/core';
import flatpickr from 'flatpickr';
import { BehaviorSubject, Subscription } from 'rxjs';
import { Dutch } from 'flatpickr/dist/l10n/nl';
import { German } from 'flatpickr/dist/l10n/de';
import { english } from 'flatpickr/dist/l10n/default';
import * as i0 from "@angular/core";
import * as i1 from "@ngx-translate/core";
import * as i2 from "@angular/common";
import * as i3 from "../input-label/input-label.component";
import * as i4 from "@angular/forms";
import * as i5 from "carbon-components-angular";
/**
 * @deprecated Migrate old design to Carbon
 */
export class DatePickerComponent {
    set fullWidth(value) {
        this.fullWidthClass = value;
    }
    constructor(translateService) {
        this.translateService = translateService;
        this.fullWidthClass = false;
        this.name = '';
        this.title = '';
        this.titleTranslationKey = '';
        this.margin = false;
        this.disabled = false;
        this.tooltip = '';
        this.required = false;
        this.smallLabel = false;
        this.enableTime = false;
        this.carbonTheme = 'g10';
        this.valueChange = new EventEmitter();
        this.dateValue$ = new BehaviorSubject('');
        this._subscriptions = new Subscription();
        this.onChange = (selectedDates, dateStr) => {
            this.dateValue$.next(dateStr);
        };
    }
    ngAfterViewInit() {
        this.openLocaleSubscription();
        this.openDateValueSubscription();
        this.openClearSubscription();
    }
    ngOnDestroy() {
        this._subscriptions.unsubscribe();
    }
    openLocaleSubscription() {
        this._subscriptions.add(this.translateService.stream('key').subscribe(() => {
            this.setFlatpickrInstance(this.translateService.currentLang);
        }));
    }
    openDateValueSubscription() {
        this._subscriptions.add(this.dateValue$.subscribe(dateValue => {
            this.valueChange.emit(dateValue);
        }));
    }
    setFlatpickrInstance(localeKey) {
        this._flatpickrInstance?.destroy();
        this._flatpickrInstance = flatpickr(this.datePickerElement.nativeElement, {
            locale: this.getLocale(localeKey),
            onChange: [this.onChange],
            defaultDate: this.getFlatpickrValue(),
            enableTime: this.enableTime,
        });
        this.emitDate();
    }
    emitDate() {
        const currentDate = this._flatpickrInstance.selectedDates[0];
        const formattedDate = currentDate && this._flatpickrInstance.formatDate(currentDate, 'Y-m-d');
        if (formattedDate) {
            this.dateValue$.next(formattedDate);
        }
    }
    getFlatpickrValue() {
        const savedValue = this.dateValue$.getValue();
        const todayDate = this.defaultDateIsToday && new Date();
        const defaultDate = this.defaultDate;
        return todayDate || defaultDate || savedValue;
    }
    getLocale(localeKey) {
        let locale;
        switch (localeKey) {
            case 'nl':
                locale = Dutch;
                break;
            case 'de':
                locale = German;
                break;
            case 'en':
                locale = english;
                break;
            default:
                locale = english;
                break;
        }
        return locale;
    }
    openClearSubscription() {
        if (this.clear$) {
            this._subscriptions.add(this.clear$.subscribe(() => {
                this.dateValue$.next('');
            }));
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: DatePickerComponent, deps: [{ token: i1.TranslateService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: DatePickerComponent, selector: "v-date-picker", inputs: { name: "name", title: "title", titleTranslationKey: "titleTranslationKey", widthPx: "widthPx", fullWidth: "fullWidth", margin: "margin", disabled: "disabled", tooltip: "tooltip", required: "required", defaultDate: "defaultDate", defaultDateIsToday: "defaultDateIsToday", smallLabel: "smallLabel", clear$: "clear$", enableTime: "enableTime", carbonTheme: "carbonTheme" }, outputs: { valueChange: "valueChange" }, host: { properties: { "class.full-width": "this.fullWidthClass" } }, viewQueries: [{ propertyName: "datePickerElement", first: true, predicate: ["datePickerElement"], descendants: true }], ngImport: i0, template: "<!--\n  ~ Copyright 2015-2024 Ritense BV, the Netherlands.\n  ~\n  ~ Licensed under EUPL, Version 1.2 (the \"License\");\n  ~ you may not use this file except in compliance with the License.\n  ~ You may obtain a copy of the License at\n  ~\n  ~ https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n  ~\n  ~ Unless required by applicable law or agreed to in writing, software\n  ~ distributed under the License is distributed on an \"AS IS\" basis,\n  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  ~ See the License for the specific language governing permissions and\n  ~ limitations under the License.\n  -->\n\n<div class=\"v-date-picker-container\">\n  <ng-container *ngTemplateOutlet=\"inputTitle\"></ng-container>\n  <ng-container *ngTemplateOutlet=\"dateInput\"></ng-container>\n</div>\n\n<ng-template #inputTitle>\n  <v-input-label\n    *ngIf=\"title || titleTranslationKey\"\n    [name]=\"name\"\n    [required]=\"required\"\n    [small]=\"smallLabel\"\n    [titleTranslationKey]=\"titleTranslationKey\"\n    [title]=\"title\"\n    [tooltip]=\"tooltip\"\n  >\n  </v-input-label>\n</ng-template>\n\n<ng-template #dateInput>\n  <input\n    #datePickerElement\n    cdsText\n    [ngClass]=\"{\n      'v-date-picker--full-width': fullWidthClass,\n      'v-date-picker--margin': margin,\n    }\"\n    [ngModel]=\"dateValue$ | async\"\n    [disabled]=\"disabled\"\n    [style.width.px]=\"widthPx + 20\"\n    type=\"text\"\n  />\n</ng-template>\n", styles: [".full-width{width:100%}.v-date-picker-container{display:flex;flex-direction:column}.v-date-picker{all:unset;font-size:var(--v-input-font-size);line-height:var(--v-input-line-height);color:var(--v-input-color);font-weight:var(--v-input-font-weight);padding-block-start:var(--v-input-padding-block);padding-block-end:var(--v-input-padding-block);padding-inline-start:var(--v-input-padding-inline);padding-inline-end:var(--v-input-padding-inline);background-color:var(--v-input-bg-color);border-radius:var(--v-input-border-radius);border:var(--v-input-border);transition:var(--v-input-transition);width:var(--v-input-default-width);box-sizing:border-box}.v-date-picker:focus{outline:var(--v-input-focus-border);outline-offset:var(--v-outline-offset)}.v-date-picker:disabled{color:var(--v-input-font-color-disabled);cursor:var(--v-input-disabled-cursor)}.v-date-picker--full-width{width:100%}.v-date-picker--margin{margin-block-end:var(--v-input-margin)}\n/*!\n * Copyright 2015-2024 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n"], dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: i3.InputLabelComponent, selector: "v-input-label", inputs: ["name", "tooltip", "largeMargin", "small", "noMargin", "title", "titleTranslationKey", "required", "disabled", "carbonTheme"] }, { kind: "directive", type: i4.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i4.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i4.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i5.TextInput, selector: "[cdsText], [ibmText]", inputs: ["theme", "size", "invalid", "warn", "skeleton"] }, { kind: "pipe", type: i2.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: DatePickerComponent, decorators: [{
            type: Component,
            args: [{ selector: 'v-date-picker', template: "<!--\n  ~ Copyright 2015-2024 Ritense BV, the Netherlands.\n  ~\n  ~ Licensed under EUPL, Version 1.2 (the \"License\");\n  ~ you may not use this file except in compliance with the License.\n  ~ You may obtain a copy of the License at\n  ~\n  ~ https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n  ~\n  ~ Unless required by applicable law or agreed to in writing, software\n  ~ distributed under the License is distributed on an \"AS IS\" basis,\n  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  ~ See the License for the specific language governing permissions and\n  ~ limitations under the License.\n  -->\n\n<div class=\"v-date-picker-container\">\n  <ng-container *ngTemplateOutlet=\"inputTitle\"></ng-container>\n  <ng-container *ngTemplateOutlet=\"dateInput\"></ng-container>\n</div>\n\n<ng-template #inputTitle>\n  <v-input-label\n    *ngIf=\"title || titleTranslationKey\"\n    [name]=\"name\"\n    [required]=\"required\"\n    [small]=\"smallLabel\"\n    [titleTranslationKey]=\"titleTranslationKey\"\n    [title]=\"title\"\n    [tooltip]=\"tooltip\"\n  >\n  </v-input-label>\n</ng-template>\n\n<ng-template #dateInput>\n  <input\n    #datePickerElement\n    cdsText\n    [ngClass]=\"{\n      'v-date-picker--full-width': fullWidthClass,\n      'v-date-picker--margin': margin,\n    }\"\n    [ngModel]=\"dateValue$ | async\"\n    [disabled]=\"disabled\"\n    [style.width.px]=\"widthPx + 20\"\n    type=\"text\"\n  />\n</ng-template>\n", styles: [".full-width{width:100%}.v-date-picker-container{display:flex;flex-direction:column}.v-date-picker{all:unset;font-size:var(--v-input-font-size);line-height:var(--v-input-line-height);color:var(--v-input-color);font-weight:var(--v-input-font-weight);padding-block-start:var(--v-input-padding-block);padding-block-end:var(--v-input-padding-block);padding-inline-start:var(--v-input-padding-inline);padding-inline-end:var(--v-input-padding-inline);background-color:var(--v-input-bg-color);border-radius:var(--v-input-border-radius);border:var(--v-input-border);transition:var(--v-input-transition);width:var(--v-input-default-width);box-sizing:border-box}.v-date-picker:focus{outline:var(--v-input-focus-border);outline-offset:var(--v-outline-offset)}.v-date-picker:disabled{color:var(--v-input-font-color-disabled);cursor:var(--v-input-disabled-cursor)}.v-date-picker--full-width{width:100%}.v-date-picker--margin{margin-block-end:var(--v-input-margin)}\n/*!\n * Copyright 2015-2024 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n"] }]
        }], ctorParameters: () => [{ type: i1.TranslateService }], propDecorators: { fullWidthClass: [{
                type: HostBinding,
                args: ['class.full-width']
            }], datePickerElement: [{
                type: ViewChild,
                args: ['datePickerElement']
            }], name: [{
                type: Input
            }], title: [{
                type: Input
            }], titleTranslationKey: [{
                type: Input
            }], widthPx: [{
                type: Input
            }], fullWidth: [{
                type: Input
            }], margin: [{
                type: Input
            }], disabled: [{
                type: Input
            }], tooltip: [{
                type: Input
            }], required: [{
                type: Input
            }], defaultDate: [{
                type: Input
            }], defaultDateIsToday: [{
                type: Input
            }], smallLabel: [{
                type: Input
            }], clear$: [{
                type: Input
            }], enableTime: [{
                type: Input
            }], carbonTheme: [{
                type: Input
            }], valueChange: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 