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: '8451ebd3c5f90040249df84fc5163639ec3e74a4', style: { fontSize: this.size } }, h("div", { key: '48f80eef34d4069f4453e60ccaa50adbf358ec3b', 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: 'fe50d713425c4c3635cab0c375bfd0b51c4aa1a2', src: this.image, alt: this.letters, onLoad: () => this.onLoadImage(), onError: () => this.onErrorImage() }), h("span", { key: 'd0ed416989aaf1ae2f110666b55010ec693a85f0' }, this.letters.substring(0, 2))), h("slot", { key: '3084db0d6fd0f335b648f454d34d044a3d8df825' }));
    }
    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": {}
        };
    }
}
