import { h } from "@stencil/core";
import { buildChoiceGroup } from "@boldpenguin/sdk-utility";
export class BpSdkBusinessClassificationSelectorList {
    constructor() {
        this.calculateGroupedChoices = (choices = []) => {
            var _a, _b;
            const topChoices = [...choices]
                .sort((a, b) => (b.score || 0) - (a.score || 0))
                .slice(0, ((_a = this.groupedChoicesConfig) === null || _a === void 0 ? void 0 : _a.maxResults) || 7);
            this.groupedChoices = buildChoiceGroup(topChoices);
            this.highestChoiceScore = ((_b = this.groupedChoices[0]) === null || _b === void 0 ? void 0 : _b.score) || 1000;
        };
        this.choiceId = undefined;
        this.choices = [];
        this.showConfidence = undefined;
        this.showNaicsCode = undefined;
        this.groupedChoicesConfig = undefined;
        this.useExternal = false;
        this.groupedChoices = [];
        this.highestChoiceScore = undefined;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-business-classification-selector-list');
        customElements.whenDefined('bp-sdk-external-business-classification-selector-list').then(() => {
            this.useExternal = true;
        });
        this.calculateGroupedChoices(this.choices);
    }
    onChoicesChange(choices) {
        this.calculateGroupedChoices(choices);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-business-classification-selector-list", { choices: this.choices, choiceId: this.choiceId, showConfidence: this.showConfidence, showNaicsCode: this.showNaicsCode, groupedChoicesConfig: this.groupedChoicesConfig }));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-business-classification-selector-list__container" }, this.groupedChoices.map((choiceGroup) => {
            var _a, _b;
            return (h("bp-sdk-business-classification-selector-naics-group", { showNaicsCode: this.showNaicsCode, showConfidence: this.showConfidence, naicsCode: choiceGroup.alias, naicsExplanation: ((_a = choiceGroup.choices[0]) === null || _a === void 0 ? void 0 : _a.explanation) || '', naicsDescription: ((_b = choiceGroup.choices[0]) === null || _b === void 0 ? void 0 : _b.national_industry_description) || '', choices: choiceGroup.choices, choiceId: this.choiceId, highestChoiceScore: this.highestChoiceScore }));
        })));
    }
    static get is() { return "bp-sdk-business-classification-selector-list"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-business-classification-selector-list.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-business-classification-selector-list.css"]
        };
    }
    static get properties() {
        return {
            "choiceId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "*\nSelected choice id"
                },
                "attribute": "choice_id",
                "reflect": false
            },
            "choices": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IChoice[]",
                    "resolved": "IChoice[]",
                    "references": {
                        "IChoice": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IChoice"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "*\nChoices to display"
                },
                "defaultValue": "[]"
            },
            "showConfidence": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Should show confidence"
                },
                "attribute": "show-confidence",
                "reflect": false
            },
            "showNaicsCode": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Should show confidence"
                },
                "attribute": "show-naics-code",
                "reflect": false
            },
            "groupedChoicesConfig": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IGroupedChoicesConfig",
                    "resolved": "IGroupedChoicesConfig",
                    "references": {
                        "IGroupedChoicesConfig": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IGroupedChoicesConfig"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Grouped choices config"
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "groupedChoices": {},
            "highestChoiceScore": {}
        };
    }
    static get watchers() {
        return [{
                "propName": "choices",
                "methodName": "onChoicesChange"
            }];
    }
}
