import { Host, h } from "@stencil/core";
import { cls } from "../../utils/helpers";
import Colors from "../../constants/colors";
export class XAvatar {
    constructor() {
        this.image = undefined;
        this.letters = '?';
        this.size = '3rem';
        this.rounded = false;
        this.imageLoaded = false;
        this.imageError = false;
    }
    /**
     * Get a number index by letter
     *
     * @returns
     */
    getLetterNumber() {
        const text = (this.letters || 'A').toUpperCase();
        const size = (this.letters || 'A').length;
        let number = 0;
        for (let position = 0; position < size; position++) {
            number += (text.charCodeAt(position) - 64) * Math.pow(26, size - position - 1);
        }
        return number;
    }
    /**
     * Get the color relative to the letters
     *
     * @returns
     */
    getColorByLetters() {
        const list = Colors.filter(color => color != 'default');
        return list[this.getLetterNumber() % list.length];
    }
    /**
     * Callback on finished loaded image
     *
     * @param event
     */
    onLoadImage() {
        this.imageLoaded = true;
    }
    /**
     * Callback error on loaded image
     *
     * @param event
     */
    onErrorImage() {
        this.imageError = true;
    }
    /**
     * Render component
     */
    render() {
        var _a;
        return h(Host, { key: '5a15cb541a91c3847838df6245aed83ae5926e36', style: { fontSize: this.size } }, h("div", { key: 'a4add7ad61afd35c666f0ad5c10b1aec912731d9', class: cls(`avatar-${this.getColorByLetters()}`, {
                'avatar-only-letters': !this.image,
                'avatar-image-loaded': this.imageLoaded && !this.imageError,
                'avatar-rounded': this.rounded,
            }) }, ((_a = this.image) === null || _a === void 0 ? void 0 : _a.length) && h("img", { key: '46609742c1c6fc2f99f5263f60b1b7b83a5ab3ff', src: this.image, alt: this.letters, onLoad: () => this.onLoadImage(), onError: () => this.onErrorImage() }), h("span", { key: 'f64ea1d2f55a6c4d1b184dec8dadfd0e0ce02b58' }, this.letters.substring(0, 2))), h("slot", { key: 'af97d070aacc150a185ac6e58f7b81342243b4fc' }));
    }
    static get is() { return "x-avatar"; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "image": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element image path"
                },
                "attribute": "image",
                "reflect": false
            },
            "letters": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Initial letters or name"
                },
                "attribute": "letters",
                "reflect": false,
                "defaultValue": "'?'"
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element font size"
                },
                "attribute": "size",
                "reflect": false,
                "defaultValue": "'3rem'"
            },
            "rounded": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element rounded style"
                },
                "attribute": "rounded",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "imageLoaded": {},
            "imageError": {}
        };
    }
}
