import { Parser, BindingEngine } from "aurelia-binding";
import { inject } from "aurelia-framework";


@inject(Parser, BindingEngine)
export class GetValueConverter {
    constructor(private parser: Parser, private bindingEngine: BindingEngine) {     
    }
    signals = ["translation-changed"];
    toView(_: any, model: any, propertyExpression: string): any {
        if (model != null) {
            let getExp = this.parser.parse(propertyExpression);
            return getExp.evaluate({ bindingContext: model, overrideContext: null });
        }
        return null;
    }
    
    fromView(value: any, model: any, propertyExpression: string): any {
        let setExp = this.parser.parse(propertyExpression + ' = $value');
        setExp.evaluate({
          bindingContext: model,
          overrideContext: { bindingContext: { $value: value }, parentOverrideContext: null }
        });
        
        return model;
    }
}