import { TranslateService } from '@ema/core';
export class TranslateComponent {
    constructor() {
        this.paramsJson = '{}';
        this.transform = 'none';
        this.translate = TranslateService.instance;
    }
    componentDidLoad() {
        this.postRender();
    }
    componentDidUpdate() {
        this.postRender();
    }
    async postRender() {
        let str = this.el.querySelector('span').innerText.trim();
        if (this.string && this.string !== '') {
            str = this.string;
        }
        if (this.language && this.language !== this.translate.language) {
            if (this.translationsLocation) {
                this.translate.translationsLocation = this.translationsLocation;
            }
            await this.translate.use(this.language);
        }
        this.translation = this.translate.get(str);
        if (this.params) {
            this.translation = this.translate.get(str, this.params);
        }
        if (this.paramsJson !== '{}') {
            let args = [str];
            let params = JSON.parse(this.paramsJson);
            params.forEach((v) => {
                args.push(v);
            });
            this.translation = this.translate.get.apply(this.translate, args);
        }
        if (this.transform) {
            this.transformTranslation(this.transform);
        }
        this.el.querySelector('span').innerText = this.translation;
    }
    async get(string) {
        return this.translate.get(string);
    }
    async set(string, language, transform) {
        this.translation = await this.translate.get(string);
        this.postRender();
    }
    async setTranslateService(instance) {
        this.translate = instance;
        this.postRender();
    }
    async update() {
        this.postRender();
    }
    transformTranslation(type) {
        if (type === 'lowercase') {
            this.translation = this.translation.toLowerCase();
        }
        if (type === 'lowercaseFirst') {
            this.translation = this.translation.charAt(0).toLowerCase() + this.translation.slice(1);
        }
        if (type === 'uppercase') {
            this.translation = this.translation.toUpperCase();
        }
        if (type === 'uppercaseFirst') {
            this.translation = this.translation.charAt(0).toUpperCase() + this.translation.slice(1);
        }
    }
    render() {
        return (h("span", null,
            h("slot", null)));
    }
    static get is() { return "ema-translate"; }
    static get properties() { return {
        "el": {
            "elementRef": true
        },
        "get": {
            "method": true
        },
        "language": {
            "type": String,
            "attr": "language"
        },
        "params": {
            "type": String,
            "attr": "params"
        },
        "paramsJson": {
            "type": String,
            "attr": "params-json"
        },
        "set": {
            "method": true
        },
        "setTranslateService": {
            "method": true
        },
        "string": {
            "type": String,
            "attr": "string"
        },
        "transform": {
            "type": String,
            "attr": "transform"
        },
        "translation": {
            "state": true
        },
        "translationsLocation": {
            "type": String,
            "attr": "translations-location"
        },
        "update": {
            "method": true
        }
    }; }
}
