import { getIcon } from "../../util/limetype";
import { getObjectUrl, ObjectResolver, } from "../../util/object-resolver";
import { PlatformServiceName, } from "@limetech/lime-web-components";
import { getElement, h } from "@stencil/core";
/**
 * Object chip
 *
 * Renders a `limel-chip` representing a Lime CRM object.
 * The consumer provides `limetype` and `objectid`, and the component
 * takes care of resolving the descriptive text, limetype icon and color,
 * and generating a navigation URL.
 *
 * :::note
 * If `platform` is not supplied as a prop, it will be fetched from the DOM.
 * :::
 *
 * @exampleComponent limebb-example-object-chip
 * @exampleComponent limebb-example-object-chip-icon-and-color-fallbacks
 * @exampleComponent limebb-example-object-chip-size
 * @beta
 */
export class ObjectChip {
    constructor() {
        /**
         * Platform instance. If not provided, the component will
         * walk the DOM to find a parent with a platform.
         *
         * :::note
         * The platform must be available when the component connects to
         * the DOM, either as a prop or via a parent element. Changing
         * the platform after the component has connected is not supported.
         * :::
         */
        /**
         * Defines the size of the chip.
         */
        this.size = 'default';
        this.data = { loadFailed: false };
        this.resolver = new ObjectResolver((data) => {
            this.data = data;
        });
    }
    connectedCallback() {
        this.resolver.connect(this.limetype, this.objectid, getElement(this), this.platform);
    }
    disconnectedCallback() {
        this.resolver.disconnect();
    }
    handlePropChange() {
        this.resolver.update(this.limetype, this.objectid);
    }
    render() {
        if (!this.resolver.resolvedPlatform) {
            return;
        }
        const icon = getIcon(this.data.limetypeData);
        return (h("limel-chip", { text: this.getChipText(), icon: icon, size: this.size, link: { href: getObjectUrl(this.limetype, this.objectid) }, loading: !this.data.limeobject && !this.data.loadFailed }));
    }
    getChipText() {
        var _a;
        if ((_a = this.data.limeobject) === null || _a === void 0 ? void 0 : _a.descriptive) {
            return this.data.limeobject.descriptive;
        }
        if (this.data.loadFailed) {
            return this.getErrorText();
        }
        return '';
    }
    getErrorText() {
        if (!this.resolver.resolvedPlatform) {
            return 'loading failed';
        }
        try {
            const translator = this.resolver.resolvedPlatform.get(PlatformServiceName.Translate);
            return translator.get('webclient.limeobject.errors.load-failed');
        }
        catch (_a) {
            return 'loading failed';
        }
    }
    static get is() { return "limebb-object-chip"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["object-chip.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["object-chip.css"]
        };
    }
    static get properties() {
        return {
            "limetype": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Name of the limetype of the object."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "limetype"
            },
            "objectid": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Id of the object."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "objectid"
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'small' | 'default'",
                    "resolved": "\"default\" | \"small\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Defines the size of the chip."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "size",
                "defaultValue": "'default'"
            },
            "platform": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentPlatform",
                    "resolved": "LimeWebComponentPlatform | undefined",
                    "references": {
                        "LimeWebComponentPlatform": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentPlatform",
                            "referenceLocation": "LimeWebComponentPlatform"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get states() {
        return {
            "data": {}
        };
    }
    static get watchers() {
        return [{
                "propName": "limetype",
                "methodName": "handlePropChange"
            }, {
                "propName": "objectid",
                "methodName": "handlePropChange"
            }];
    }
}
