import { Config } from '../config';
import { Datacontext } from './../data/data-context';
import { Container, autoinject } from "aurelia-framework";

@autoinject
export class TranslatedPropertyHelper {
    constructor(public datacontext: Datacontext) { }

    public static createMissingProperty(object: Object) {
        if (object == null) {
            object = {};
        }
        let config = Container.instance.get(Config);
        config.globalConfig.configuredLanguages.forEach(
            language => {
                let prop = language._columnName;
                object[prop] ??= '';
            }
        )
        return object;
    }

    public addNecessaryProperties(object: Object, addSourceProperty: boolean = true) {
        Object.defineProperty(object, '_translation',
            {
                get: () => {
                    return object[this.datacontext.translatePropertyName];
                },
                set: (val) => {
                    object[this.datacontext.translatePropertyName] = val;
                }
            }
        );
        if (addSourceProperty) {
            Object.defineProperty(object, '_sourceProperty',
                {
                    get: () => {
                        return this.datacontext.translatePropertyName;
                    }
                }
            );
        }
    }
}