import InnerNode from "./InnerNode";
import EventHandler from "../Utility/EventHandler";

export default interface DataNode<T> {
    onChange: EventHandler<ChangeEvent<any>>;
    onValidChange: EventHandler<ValidEvent>;
    onServerSideErrorsChange: EventHandler<ServerSideErrorEvent>;
    validate(force:boolean): void;
    isValid: boolean;
    isVisible: boolean;
    parent: null|InnerNode;
    index: string|number|null;
    get isChanged(): boolean;
    get value(): T|null|undefined;
    set value(value: T|null|undefined);
    get valueOrDefault(): T|null;
    set serverSideErrors(errors: any);
    get serverSideErrors(): any[];
}

export class ChangeEvent<UpdatedValue>{
    public constructor(
        public readonly dataNode: DataNode<UpdatedValue>,
        public readonly value: UpdatedValue|null|undefined = undefined
    ){}
}

export class ValidEvent{
    public constructor(
        public readonly isValueValid: boolean,
        public readonly isVisible: boolean
    ){}

    public get isValid(): boolean{
        return !this.isVisible || this.isValueValid;
    }
}

export class ServerSideErrorEvent{
    public constructor(
        public readonly errors: any[]
    ){}
}