import { Cache } from '../utils/exported';
import { IBindingContext } from '../interface/exported';
/**
 * get context object
 *
 */
export function getCorrectContext(value: any, ctx: IBindingContext): IBindingContext {

    const keys = Cache.keyMaps.getCreate(value);
    const key = keys[0];


    // more then 1, then its array
    let newCtx = ctx;
    let overrideCtx: any;


    if (typeof key === 'string' && key[0] !== '$') {
        if (newCtx && newCtx.$overrideContext) {
            while (newCtx.$overrideContext) {
                newCtx = newCtx.$overrideContext;
                if (newCtx && newCtx.$context && key in newCtx.$context) {
                    overrideCtx = newCtx;
                }
            }
        }
    }

    return overrideCtx || ctx;
}
