import * as i0 from '@angular/core';
import { Directive, Input } from '@angular/core';
import { isNullOrUndefined, isNumber } from '@ironsource/fusion-ui/utils';
import { BehaviorSubject } from 'rxjs';

/*
 * Created on 2021.3.14 By Andy Kononenko (andyk@ironsrc.com)
 */
const MONTH_NAMES_SHORT = {
    en: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
};
const MONTH_PICKER_PLACEHOLDER = { text: 'Select', prefix: 'Month:', format: 'MMM yyyy' };

class MonthPickerBaseComponent {
    constructor() {
        this.error = '';
        this.config = {
            placeholder: this.setPlaceholder()
        };
        this.isOpen$ = new BehaviorSubject(false);
        this.isDisabled = false;
        this.propagateChange = (_) => { };
        this.propagateTouched = () => { };
    }
    set configuration(value) {
        this.config.placeholder = this.setPlaceholder(value.placeholder);
        this.config.min = value.min;
        this.config.max = value.max;
    }
    set disabled(value) {
        this.setDisabledState(value);
    }
    onOutsideClick() {
        if (this.isOpen$.getValue()) {
            this.isOpen$.next(false);
        }
    }
    onMonthPlaceholderClicked() {
        if (!this.isDisabled) {
            this.isOpen$.next(!this.isOpen$.getValue());
        }
        this.propagateTouched();
    }
    selectedChanged(selected) {
        this.propagateTouched();
        this.selected = selected;
        this.propagateChange(this.selected);
    }
    writeValue(value) {
        if (!isNullOrUndefined(value) && isNumber(value.year) && isNumber(value.month)) {
            this.selected = { year: value.year, month: value.month };
        }
        else {
            this.selected = null;
        }
    }
    registerOnChange(fn) {
        this.propagateChange = fn;
    }
    registerOnTouched(fn) {
        this.propagateTouched = fn;
    }
    setDisabledState(isDisabled) {
        this.isDisabled = isDisabled;
    }
    setPlaceholder(placeholder) {
        const defaultPlaceholder = MONTH_PICKER_PLACEHOLDER;
        if (!isNullOrUndefined(placeholder)) {
            if (typeof placeholder === 'string') {
                return { ...defaultPlaceholder, text: placeholder };
            }
            else {
                return {
                    text: placeholder.text ?? defaultPlaceholder.text,
                    prefix: placeholder.prefix ?? defaultPlaceholder.prefix,
                    format: placeholder.format ?? defaultPlaceholder.format
                };
            }
        }
        return defaultPlaceholder;
    }
    static { this.ɵfac = function MonthPickerBaseComponent_Factory(t) { return new (t || MonthPickerBaseComponent)(); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: MonthPickerBaseComponent, inputs: { configuration: "configuration", disabled: "disabled", error: "error" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MonthPickerBaseComponent, [{
        type: Directive
    }], null, { configuration: [{
            type: Input
        }], disabled: [{
            type: Input
        }], error: [{
            type: Input
        }] }); })();

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

export { MONTH_NAMES_SHORT, MONTH_PICKER_PLACEHOLDER, MonthPickerBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-month-picker-common-base.mjs.map
