import {Directive, Host, Attribute, OnInit,  Optional} from "@angular/core";
import {Validators} from "@angular/forms";
import {CoreFormControlBase} from "../CoreFormControlBase";
import {CoreFormInput} from "../core-form-input/core-form-input.component";
import {CoreFormDropDown} from "../core-form-dropdown/core-form-dropdown";

// const provideParent =
//     ( component:any, parentType?:any ) =>
//     {
//         return {provide: parentType || CoreFormInput, useExisting: forwardRef(() => component)};
//     };

@Directive(
    {
        selector: 'validation-required'
    }
)
export class CoreFormValidationRequired implements OnInit
{
    private host:CoreFormControlBase;

    ngOnInit():any
    {
        if (this.host != null)
            this.host.addFormValidator({
                                           ValidationName: 'required',
                                           ValidationMessage: this.validationRequiredMessage
                                       }, Validators.required);
        return undefined;
    }

    constructor( @Optional() @Host() private inputHost:CoreFormInput,
                 @Optional() @Host() private dropDownHost:CoreFormDropDown,
                 @Attribute("message") private validationRequiredMessage:string )
    {
        this.host = inputHost || dropDownHost;
    }

}