import { __decorate, __metadata } from "tslib";
import { ChangeDetectorRef, Directive, ElementRef, Inject, Injector, Input, Optional } from '@angular/core';
import { AbstractControl, FormControl, Validators } from '@angular/forms';
import { LayoutComponentBase } from '@anglr/dynamic/layout';
import { LOGGER } from '@anglr/common';
import { DynamicOutput } from '@anglr/dynamic/relations';
import { FORM_COMPONENT_CONTROL } from '../../misc/tokens';
import { FormComponentControlType } from '../../misc/enums';
import { getControlForType, getFormControl } from '../../misc/utils';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
/**
 * Base component for form component
 */
export class FormComponentBase extends LayoutComponentBase {
    //######################### public properties - inputs #########################
    /**
     * Form component value
     */
    set value(value) {
        const oldValue = this.value;
        this.valueOutput = value;
        if (value !== oldValue) {
            this.control?.patchValue(value);
        }
    }
    get value() {
        return this.valueOutput;
    }
    /**
     * Indication whether form component is required
     */
    set required(value) {
        this._required = value;
        if (value === true) {
            this.control?.addValidators(Validators.required);
            return;
        }
        this.control?.removeValidators(Validators.required);
    }
    get required() {
        return this._required;
    }
    /**
     * Indication whether form component is disabled
     */
    set disabled(value) {
        this._disabled = value;
        if (value === true) {
            this.control?.disable();
            return;
        }
        this.control?.enable();
    }
    get disabled() {
        return this._disabled;
    }
    //######################### constructor #########################
    constructor(_changeDetector, _element, _injector, _parentControl, _logger) {
        super(_changeDetector, _element, _injector, _logger);
        this._parentControl = _parentControl;
        //######################### protected properties #########################
        /**
         * Form component control
         */
        this.control = new FormControl();
    }
    //######################### protected methods - _onOptionsSet implementation #########################
    /**
     * @inheritdoc
     */
    onOptionsSet() {
        if (!this.options) {
            return;
        }
        this.controlSubscription?.unsubscribe();
        this.controlSubscription = null;
        this.control = this._getFormControl(this.options?.controlName, this._parentControl, FormComponentControlType.FormControl, this.value);
        this._registerValidations();
        this.controlSubscription = this.control
            ?.valueChanges
            ?.subscribe(value => {
            this.valueOutput = value;
        });
    }
    //######################### protected methods #########################
    /**
     * Registers component-specific validators
     */
    _registerValidations() { }
    //######################### private methods #########################
    /**
     * Gets form component control for control name
     * @param controlName Form component control name
     * @param parentControl Parent form control
     * @param defaultControlType Default control type when parent does not exists or not contains control with specified name
     * @param initValue Initial value for control
     * @returns Specified type of control
     */
    _getFormControl(controlName, parentControl, defaultControlType = FormComponentControlType.FormControl, initValue) {
        let control = getFormControl(controlName, parentControl, defaultControlType, initValue);
        if (!control) {
            this.logger?.warn('FormComponentBase: Unable to find control with name {{@name}}', { name: controlName });
            control = getControlForType(defaultControlType, initValue);
        }
        if (this.required) {
            control.addValidators(Validators.required);
        }
        if (this.disabled) {
            control.disable();
        }
        return control;
    }
}
FormComponentBase.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: FormComponentBase, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: i0.Injector }, { token: FORM_COMPONENT_CONTROL, optional: true }, { token: LOGGER, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
FormComponentBase.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.0.4", type: FormComponentBase, inputs: { value: "value", required: "required", disabled: "disabled" }, usesInheritance: true, ngImport: i0 });
__decorate([
    DynamicOutput(),
    __metadata("design:type", Object)
], FormComponentBase.prototype, "valueOutput", void 0);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: FormComponentBase, decorators: [{
            type: Directive
        }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: i0.Injector }, { type: i1.AbstractControl, decorators: [{
                    type: Inject,
                    args: [FORM_COMPONENT_CONTROL]
                }, {
                    type: Optional
                }] }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [LOGGER]
                }, {
                    type: Optional
                }] }], propDecorators: { value: [{
                type: Input
            }], required: [{
                type: Input
            }], disabled: [{
                type: Input
            }], valueOutput: [] } });
//# sourceMappingURL=formComponentBase.js.map