import * as i0 from '@angular/core';
import { input, numberAttribute, booleanAttribute, inject, computed, signal, effect, ChangeDetectionStrategy, Component } from '@angular/core';
import * as i1 from '@angular/forms';
import { NgControl, Validators, FormsModule } from '@angular/forms';
import { FieldValidation } from '@masterteam/components/field-validation';
import { isInvalid } from '@masterteam/components';
import * as i2 from 'primeng/inputgroup';
import { InputGroupModule } from 'primeng/inputgroup';
import * as i3 from 'primeng/inputgroupaddon';
import { InputGroupAddonModule } from 'primeng/inputgroupaddon';
import * as i4 from 'primeng/inputtext';
import { InputTextModule } from 'primeng/inputtext';

let nextUniqueId = 0;
class SliderField {
    field = input(true, ...(ngDevMode ? [{ debugName: "field" }] : /* istanbul ignore next */ []));
    label = input(...(ngDevMode ? [undefined, { debugName: "label" }] : /* istanbul ignore next */ []));
    class = input('', ...(ngDevMode ? [{ debugName: "class" }] : /* istanbul ignore next */ []));
    min = input(0, { ...(ngDevMode ? { debugName: "min" } : /* istanbul ignore next */ {}), transform: numberAttribute });
    max = input(100, { ...(ngDevMode ? { debugName: "max" } : /* istanbul ignore next */ {}), transform: numberAttribute });
    step = input(undefined, { ...(ngDevMode ? { debugName: "step" } : /* istanbul ignore next */ {}), transform: numberAttribute });
    hideNumber = input(false, { ...(ngDevMode ? { debugName: "hideNumber" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
    unit = input('%', ...(ngDevMode ? [{ debugName: "unit" }] : /* istanbul ignore next */ []));
    readonly = input(false, ...(ngDevMode ? [{ debugName: "readonly" }] : /* istanbul ignore next */ []));
    required = input(false, ...(ngDevMode ? [{ debugName: "required" }] : /* istanbul ignore next */ []));
    ngControl = inject(NgControl, { optional: true, self: true });
    hostClass = computed(() => ['grid gap-1', this.class()].filter(Boolean).join(' '), ...(ngDevMode ? [{ debugName: "hostClass" }] : /* istanbul ignore next */ []));
    fieldId = computed(() => {
        const labelId = this.label()?.trim().toLowerCase().replace(/\s+/g, '-');
        return this.ngControl?.name?.toString() || labelId || this.defaultId;
    }, ...(ngDevMode ? [{ debugName: "fieldId" }] : /* istanbul ignore next */ []));
    progressBgStyle = computed(() => {
        const val = this.value() ?? this.min();
        const min = this.min();
        const max = this.max();
        const range = max - min;
        const pct = range > 0 ? Math.min(100, Math.max(0, ((val - min) / range) * 100)) : 0;
        return `linear-gradient(to right, color-mix(in srgb, var(--p-primary-color) 18%, transparent) ${pct}%, transparent ${pct}%)`;
    }, ...(ngDevMode ? [{ debugName: "progressBgStyle" }] : /* istanbul ignore next */ []));
    requiredValidator = Validators.required;
    value = signal(null, ...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
    disabled = signal(false, ...(ngDevMode ? [{ debugName: "disabled" }] : /* istanbul ignore next */ []));
    onTouched = () => { };
    onModelChange = () => { };
    isInvalid = isInvalid;
    defaultId = `mt-slider-field-${nextUniqueId++}`;
    constructor() {
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
        effect(() => {
            const control = this.ngControl?.control;
            if (!control) {
                return;
            }
            if (this.required()) {
                control.addValidators(Validators.required);
            }
            else {
                control.removeValidators(Validators.required);
            }
            control.updateValueAndValidity({ emitEvent: false });
        });
    }
    onValueChange(value) {
        const normalizedValue = this.normalizeValue(value);
        this.onModelChange(normalizedValue);
        this.value.set(normalizedValue);
    }
    writeValue(value) {
        this.value.set(this.normalizeValue(value));
    }
    registerOnChange(fn) {
        this.onModelChange = fn;
    }
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    setDisabledState(disabled) {
        this.disabled.set(disabled);
    }
    normalizeValue(value) {
        if (value === null || value === undefined || Number.isNaN(value)) {
            return null;
        }
        if (value < this.min()) {
            return this.min();
        }
        if (value > this.max()) {
            return this.max();
        }
        return value;
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: SliderField, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.8", type: SliderField, isStandalone: true, selector: "mt-slider-field", inputs: { field: { classPropertyName: "field", publicName: "field", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, class: { classPropertyName: "class", publicName: "class", isSignal: true, isRequired: false, transformFunction: null }, min: { classPropertyName: "min", publicName: "min", isSignal: true, isRequired: false, transformFunction: null }, max: { classPropertyName: "max", publicName: "max", isSignal: true, isRequired: false, transformFunction: null }, step: { classPropertyName: "step", publicName: "step", isSignal: true, isRequired: false, transformFunction: null }, hideNumber: { classPropertyName: "hideNumber", publicName: "hideNumber", isSignal: true, isRequired: false, transformFunction: null }, unit: { classPropertyName: "unit", publicName: "unit", isSignal: true, isRequired: false, transformFunction: null }, readonly: { classPropertyName: "readonly", publicName: "readonly", isSignal: true, isRequired: false, transformFunction: null }, required: { classPropertyName: "required", publicName: "required", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "class": "hostClass()" } }, ngImport: i0, template: "@if (label()) {\r\n  <label\r\n    [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\r\n    [for]=\"fieldId()\"\r\n    >{{ label() }}</label\r\n  >\r\n}\r\n\r\n<p-inputgroup [class.pointer-events-none]=\"disabled()\">\r\n  <input\r\n    pInputText\r\n    type=\"number\"\r\n    [ngModel]=\"value()\"\r\n    (ngModelChange)=\"onValueChange($event)\"\r\n    (blur)=\"onTouched()\"\r\n    [disabled]=\"disabled()\"\r\n    [readonly]=\"readonly()\"\r\n    [id]=\"fieldId()\"\r\n    [min]=\"min()\"\r\n    [max]=\"max()\"\r\n    [invalid]=\"isInvalid(ngControl?.control)\"\r\n    [style.background]=\"\r\n      disabled()\r\n        ? 'var(--p-inputtext-disabled-background, var(--p-surface-200))'\r\n        : progressBgStyle()\r\n    \"\r\n    class=\"slider-progress-input w-full [transition:background_0.2s_ease]\"\r\n  />\r\n  <p-inputgroup-addon>{{ unit() }}</p-inputgroup-addon>\r\n</p-inputgroup>\r\n\r\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\r\n", styles: [":host ::ng-deep .slider-progress-input::-webkit-inner-spin-button,:host ::ng-deep .slider-progress-input::-webkit-outer-spin-button{-webkit-appearance:none;margin:0}:host ::ng-deep .slider-progress-input[type=number]{-moz-appearance:textfield;appearance:textfield}\n"], dependencies: [{ kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i1.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: i1.NumberValueAccessor, selector: "input[type=number][formControlName],input[type=number][formControl],input[type=number][ngModel]" }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.MinValidator, selector: "input[type=number][min][formControlName],input[type=number][min][formControl],input[type=number][min][ngModel]", inputs: ["min"] }, { kind: "directive", type: i1.MaxValidator, selector: "input[type=number][max][formControlName],input[type=number][max][formControl],input[type=number][max][ngModel]", inputs: ["max"] }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "ngmodule", type: InputGroupModule }, { kind: "component", type: i2.InputGroup, selector: "p-inputgroup, p-inputGroup, p-input-group", inputs: ["styleClass"] }, { kind: "ngmodule", type: InputGroupAddonModule }, { kind: "component", type: i3.InputGroupAddon, selector: "p-inputgroup-addon, p-inputGroupAddon", inputs: ["style", "styleClass"] }, { kind: "ngmodule", type: InputTextModule }, { kind: "directive", type: i4.InputText, selector: "[pInputText]", inputs: ["hostName", "ptInputText", "pInputTextPT", "pInputTextUnstyled", "pSize", "variant", "fluid", "invalid"] }, { kind: "component", type: FieldValidation, selector: "mt-field-validation", inputs: ["control", "touched"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: SliderField, decorators: [{
            type: Component,
            args: [{ selector: 'mt-slider-field', imports: [
                        FormsModule,
                        InputGroupModule,
                        InputGroupAddonModule,
                        InputTextModule,
                        FieldValidation,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, host: {
                        '[class]': 'hostClass()',
                    }, template: "@if (label()) {\r\n  <label\r\n    [class.required]=\"ngControl?.control?.hasValidator(requiredValidator)\"\r\n    [for]=\"fieldId()\"\r\n    >{{ label() }}</label\r\n  >\r\n}\r\n\r\n<p-inputgroup [class.pointer-events-none]=\"disabled()\">\r\n  <input\r\n    pInputText\r\n    type=\"number\"\r\n    [ngModel]=\"value()\"\r\n    (ngModelChange)=\"onValueChange($event)\"\r\n    (blur)=\"onTouched()\"\r\n    [disabled]=\"disabled()\"\r\n    [readonly]=\"readonly()\"\r\n    [id]=\"fieldId()\"\r\n    [min]=\"min()\"\r\n    [max]=\"max()\"\r\n    [invalid]=\"isInvalid(ngControl?.control)\"\r\n    [style.background]=\"\r\n      disabled()\r\n        ? 'var(--p-inputtext-disabled-background, var(--p-surface-200))'\r\n        : progressBgStyle()\r\n    \"\r\n    class=\"slider-progress-input w-full [transition:background_0.2s_ease]\"\r\n  />\r\n  <p-inputgroup-addon>{{ unit() }}</p-inputgroup-addon>\r\n</p-inputgroup>\r\n\r\n<mt-field-validation [control]=\"ngControl?.control\"></mt-field-validation>\r\n", styles: [":host ::ng-deep .slider-progress-input::-webkit-inner-spin-button,:host ::ng-deep .slider-progress-input::-webkit-outer-spin-button{-webkit-appearance:none;margin:0}:host ::ng-deep .slider-progress-input[type=number]{-moz-appearance:textfield;appearance:textfield}\n"] }]
        }], ctorParameters: () => [], propDecorators: { field: [{ type: i0.Input, args: [{ isSignal: true, alias: "field", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], class: [{ type: i0.Input, args: [{ isSignal: true, alias: "class", required: false }] }], min: [{ type: i0.Input, args: [{ isSignal: true, alias: "min", required: false }] }], max: [{ type: i0.Input, args: [{ isSignal: true, alias: "max", required: false }] }], step: [{ type: i0.Input, args: [{ isSignal: true, alias: "step", required: false }] }], hideNumber: [{ type: i0.Input, args: [{ isSignal: true, alias: "hideNumber", required: false }] }], unit: [{ type: i0.Input, args: [{ isSignal: true, alias: "unit", required: false }] }], readonly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readonly", required: false }] }], required: [{ type: i0.Input, args: [{ isSignal: true, alias: "required", required: false }] }] } });

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

export { SliderField };
//# sourceMappingURL=masterteam-components-slider-field.mjs.map
