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: '8afae1b521dd9063ac4897827b6f5b87763a4b50', style: { fontSize: this.size } }, h("div", { key: 'd57d131fdaf8a3dc4ce773ad437d3502753e0059', 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: 'c13a4dd363bc355345b4411201588b18285ebdca', src: this.image, alt: this.letters, onLoad: () => this.onLoadImage(), onError: () => this.onErrorImage() }), h("span", { key: '53efed99361231f4e68c6f36f0e2e5701bbaffbe' }, this.letters.substring(0, 2))), h("slot", { key: '0153f9d432c1e49535fa04be65bc8377fcf11be8' }));
    }
    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": {}
        };
    }
}
