import { BindingEngine } from '../binding/exported';
import { Cache } from './cache';
import { ContainerBehavior } from '../container/exported';

export function connectBehavior(expressionValue: string, _class: any) {
    if (expressionValue.indexOf('&') !== -1) {
        let x = Cache.expressionMap.get(expressionValue);
        if (!x) {
            const tokens: any = BindingEngine.tokenize(expressionValue);
            x = {};
            x.ast = BindingEngine.generateAST(tokens);
            Cache.expressionMap.set(expressionValue, {
                ast: x.ast,
                tokens: tokens
            });
        }
        const behaviors = BindingEngine.getBehavior((<any>x).ast);
        if (behaviors) {
            behaviors.forEach((behavior: { name: string, args: any[] }) => {
                const x = ContainerBehavior.findBehavior(behavior.name);
                if (x) {
                    let curBehavior = new x(_class, behavior.args);
                    curBehavior = curBehavior;
                }
            });
        }
    }
}
