var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f)
        throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
        throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m")
        throw new TypeError("Private method is not writable");
    if (kind === "a" && !f)
        throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
        throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _GxIdeEntitySelector_componentLocale, _GxIdeEntitySelector_btnClearClickHandler, _GxIdeEntitySelector_btnSelectClickHandler, _GxIdeEntitySelector_buttonFocusHandler, _GxIdeEntitySelector_renderControl, _GxIdeEntitySelector_updateIconSrc;
import { Host, h } from "@stencil/core";
import { getIconPath } from "@genexus/mercury";
import { Locale } from "../../../common/locale";
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/edit",
    "components/button",
    "components/icon",
    "utils/form"
];
const SELECT_DEFAULT_ICON = getIconPath({
    category: "gemini-tools",
    name: "show-more-horizontal",
    colorType: "primary"
});
const CLEAR_ICON = getIconPath({
    category: "gemini-tools",
    name: "reset",
    colorType: "primary"
});
export class GxIdeEntitySelector {
    constructor() {
        _GxIdeEntitySelector_componentLocale.set(this, void 0);
        _GxIdeEntitySelector_btnClearClickHandler.set(this, () => {
            var _a;
            this.value = this.defaultValue;
            this.iconSrc = (_a = this.defaultValue) === null || _a === void 0 ? void 0 : _a.iconSrc;
        });
        _GxIdeEntitySelector_btnSelectClickHandler.set(this, () => {
            this.selectEntityCallback().then(result => {
                if (result) {
                    this.value = result;
                }
            });
        });
        _GxIdeEntitySelector_buttonFocusHandler.set(this, (e) => {
            if (e.type === "focus") {
                this.buttonHasFocus = true;
            }
            else if (e.type === "blur") {
                this.buttonHasFocus = false;
            }
        });
        _GxIdeEntitySelector_renderControl.set(this, () => {
            var _a, _b;
            return (h("div", { part: "wrapper", class: "wrapper" }, h("ch-edit", { accessibleName: this.labelPosition === "none" &&
                    __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").entitySelectorInputAccessibleName, class: "input", disabled: this.disabled, name: this.name || __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").entitySelectorNameAttribute, startImgSrc: this.iconSrc, readonly: true, value: ((_a = this.value) === null || _a === void 0 ? void 0 : _a.name) || ((_b = this.defaultValue) === null || _b === void 0 ? void 0 : _b.name), id: "entity-input" }), h("button", { part: "button-clear", class: "icon-button", "aria-label": __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").clearButtonLabel, title: __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").clearButtonLabel, type: "button", disabled: this.disabled, onClick: __classPrivateFieldGet(this, _GxIdeEntitySelector_btnClearClickHandler, "f"), onFocus: this.buttonHasFocus ? __classPrivateFieldGet(this, _GxIdeEntitySelector_buttonFocusHandler, "f") : undefined, onBlur: !this.buttonHasFocus ? __classPrivateFieldGet(this, _GxIdeEntitySelector_buttonFocusHandler, "f") : undefined }, h("ch-image", { class: "icon-m", src: CLEAR_ICON, disabled: this.disabled })), h("button", { class: "icon-button", "aria-label": __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").selectButtonLabel, title: __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").selectButtonLabel, type: "button", disabled: !this.selectEntityCallback || this.disabled, onClick: __classPrivateFieldGet(this, _GxIdeEntitySelector_btnSelectClickHandler, "f"), onBlur: !this.buttonHasFocus ? __classPrivateFieldGet(this, _GxIdeEntitySelector_buttonFocusHandler, "f") : undefined, onFocus: this.buttonHasFocus ? __classPrivateFieldGet(this, _GxIdeEntitySelector_buttonFocusHandler, "f") : undefined }, h("ch-image", { class: "icon-m", src: SELECT_DEFAULT_ICON, disabled: this.disabled }))));
        });
        _GxIdeEntitySelector_updateIconSrc.set(this, () => {
            if (this.value && this.value.iconSrc) {
                this.iconSrc = this.value.iconSrc;
            }
        });
        this.buttonHasFocus = false;
        this.iconSrc = null;
        this.defaultValue = undefined;
        this.disabled = false;
        this.labelCaption = undefined;
        this.labelPosition = "block-start";
        this.name = undefined;
        this.selectEntityCallback = undefined;
        this.value = undefined;
    }
    valueChangedHandler() {
        this.valueChanged.emit(this.value);
        __classPrivateFieldGet(this, _GxIdeEntitySelector_updateIconSrc, "f").call(this);
    }
    // 6.Component Lifecycle Events
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeEntitySelector_componentLocale, await Locale.getComponentStrings(this.el), "f");
    }
    connectedCallback() {
        __classPrivateFieldGet(this, _GxIdeEntitySelector_updateIconSrc, "f").call(this);
    }
    render() {
        return (h(Host, { class: {
                "entity-selector--button-has-focus": this.buttonHasFocus
            } }, h("ch-theme", { model: CSS_BUNDLES }), this.labelPosition !== "none" ? (h("div", { class: {
                "field": true,
                "field-block": this.labelPosition === "block-start",
                "field-inline": this.labelPosition === "inline-start"
            } }, h("label", { class: "label", htmlFor: "entity-input" }, this.labelCaption ||
            __classPrivateFieldGet(this, _GxIdeEntitySelector_componentLocale, "f").entitySelectorInputAccessibleName), __classPrivateFieldGet(this, _GxIdeEntitySelector_renderControl, "f").call(this))) : (__classPrivateFieldGet(this, _GxIdeEntitySelector_renderControl, "f").call(this))));
    }
    static get is() { return "gx-ide-entity-selector"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["entity-selector.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["entity-selector.css"]
        };
    }
    static get assetsDirs() { return ["gx-ide-assets/entity-selector"]; }
    static get properties() {
        return {
            "defaultValue": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "EntityData | null | undefined",
                    "resolved": "{ id: string; name: string; iconSrc?: string; }",
                    "references": {
                        "EntityData": {
                            "location": "import",
                            "path": "../../../common/types",
                            "id": "src/common/types.ts::EntityData"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Default value for the component. Used when the 'X' button is pressed."
                }
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "This attribute lets you specify if the element is disabled."
                },
                "attribute": "disabled",
                "reflect": true,
                "defaultValue": "false"
            },
            "labelCaption": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The label caption. Only visible if \"labelPosition\" is not \"none\".\nI no \"labelCaption\" is provided, a generic caption \"Select Entity\" will be provided"
                },
                "attribute": "label-caption",
                "reflect": false
            },
            "labelPosition": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "LabelPosition",
                    "resolved": "\"block-start\" | \"inline-start\" | \"none\"",
                    "references": {
                        "LabelPosition": {
                            "location": "import",
                            "path": "../../../common/types",
                            "id": "src/common/types.ts::LabelPosition"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The label position"
                },
                "attribute": "label-position",
                "reflect": true,
                "defaultValue": "\"block-start\""
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "This property specifies the `name` of the control when used in a form."
                },
                "attribute": "name",
                "reflect": true
            },
            "selectEntityCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "() => Promise<EntityData> | null",
                    "resolved": "() => Promise<EntityData>",
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "EntityData": {
                            "location": "import",
                            "path": "../../../common/types",
                            "id": "src/common/types.ts::EntityData"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when the action button is pressed. Returns the new value."
                }
            },
            "value": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "EntityData | null | undefined",
                    "resolved": "{ id: string; name: string; iconSrc?: string; }",
                    "references": {
                        "EntityData": {
                            "location": "import",
                            "path": "../../../common/types",
                            "id": "src/common/types.ts::EntityData"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Value currently assigned."
                }
            }
        };
    }
    static get states() {
        return {
            "buttonHasFocus": {},
            "iconSrc": {}
        };
    }
    static get events() {
        return [{
                "method": "valueChanged",
                "name": "valueChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits the input value every time it changes"
                },
                "complexType": {
                    "original": "EntityData",
                    "resolved": "{ id: string; name: string; iconSrc?: string; }",
                    "references": {
                        "EntityData": {
                            "location": "import",
                            "path": "../../../common/types",
                            "id": "src/common/types.ts::EntityData"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "value",
                "methodName": "valueChangedHandler"
            }];
    }
}
_GxIdeEntitySelector_componentLocale = new WeakMap(), _GxIdeEntitySelector_btnClearClickHandler = new WeakMap(), _GxIdeEntitySelector_btnSelectClickHandler = new WeakMap(), _GxIdeEntitySelector_buttonFocusHandler = new WeakMap(), _GxIdeEntitySelector_renderControl = new WeakMap(), _GxIdeEntitySelector_updateIconSrc = new WeakMap();
//# sourceMappingURL=entity-selector.js.map
