import { h } from '@stencil/core';
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();
        // beware: string property overrides slot content
        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 {
        "language": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "language",
            "reflect": false
        },
        "string": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "string",
            "reflect": false
        },
        "params": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "params",
            "reflect": false
        },
        "paramsJson": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "params-json",
            "reflect": false,
            "defaultValue": "'{}'"
        },
        "transform": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "transform",
            "reflect": false,
            "defaultValue": "'none'"
        },
        "translationsLocation": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "translations-location",
            "reflect": false
        }
    }; }
    static get states() { return {
        "translation": {}
    }; }
    static get methods() { return {
        "get": {
            "complexType": {
                "signature": "(string: string) => Promise<string>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<string>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        },
        "set": {
            "complexType": {
                "signature": "(string: string, language: string, transform: any) => Promise<void>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }, {
                        "tags": [],
                        "text": ""
                    }, {
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        },
        "setTranslateService": {
            "complexType": {
                "signature": "(instance: any) => Promise<void>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        },
        "update": {
            "complexType": {
                "signature": "() => Promise<void>",
                "parameters": [],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        }
    }; }
    static get elementRef() { return "el"; }
}
