import { toast } from 'react-toastify';
import foucsById from "./focus";

type Tnode = {
    value: boolean,
    failMsg?: string,
    id?: string;
}

const validate = (nodes:Tnode[]):boolean => {
    return nodes.every((node)=> {
        if(!node.value) {
            if(node.failMsg) toast(node.failMsg);
            if(node.id) foucsById(node.id);
            return false;
        }

        return true;
    })
}


export class Validater {
    nodes:Tnode[]

    public validate = (checks?: number[]):boolean => {
        const targetNodes = checks?.map(num => this.nodes[num]);
        let nodes = targetNodes || this.nodes
        
        return nodes.every((node)=> {
            if(!node.value) {
                if(node.failMsg) toast(node.failMsg);
                if(node.id) foucsById(node.id);
                return false;
            }
    
            return true;
        })
    }

    constructor(nodes:Tnode[]) {
        this.nodes = nodes
    }
}

export default validate;