import { getPartnerEngineConfig, getSelectedChoices, removeActiveAnswerId, searchChoices, searchChoicesWithNudges, } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkBusinessClassificationSelectorContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { searchChoices, removeActiveAnswerId, searchChoicesWithNudges });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                remoteChoices: getSelectedChoices(state, this.questionId),
                config: getPartnerEngineConfig(state),
            };
        };
        this.answerId = undefined;
        this.questionId = undefined;
        this.code = '';
        this.placeholder = '';
        this.required = false;
        this.errors = undefined;
        this.errorText = undefined;
        this.labelId = undefined;
        this.choiceId = undefined;
        this.touched = false;
        this.pristine = true;
        this.isCompleted = false;
        this.isReadOnly = false;
        this.fieldType = undefined;
        this.label = undefined;
        this.labelledBy = undefined;
        this.choices = [];
        this.remoteChoices = [];
        this.showNaicsCode = true;
        this.showConfidence = true;
        this.config = undefined;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this.choiceMap = {};
        this._selectedChoices = [];
        this._searchChoices = [];
        this._currentSearchValue = '';
        this.useExternal = false;
        this.showChoices = false;
    }
    resetState() {
        this.showChoices = false;
        this._currentSearchValue = '';
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-business-classification-selector-container');
        customElements.whenDefined('bp-sdk-external-business-classification-selector-container').then(() => {
            this.useExternal = true;
        });
        this.onQuestionIdChange();
        this.onChoicesChange(this.choices || []);
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    onQuestionIdChange() {
        this.handleSubscriptions();
    }
    doSearchChoices() {
        if (this.useExternal) {
            return;
        }
        const params = {
            config: this.config,
            filter: this._currentSearchValue,
            questionId: this.questionId,
            timestamp: Date.now(),
            refilter: null,
        };
        this.searchChoices(params);
    }
    onFilterUpdate({ detail }) {
        if (this.useExternal) {
            return;
        }
        this.showChoices = true;
        this._currentSearchValue = detail.value;
        this.doSearchChoices();
    }
    onRefilterAnswer() {
        this.doSearchChoices();
    }
    onValueUpdate() {
        if (this.useExternal) {
            return;
        }
        this.showChoices = false;
        this.removeActiveAnswerId();
    }
    onChoicesChange(choices) {
        this.choiceMap = choices.reduce((acc, choice) => (Object.assign(Object.assign({}, acc), { [choice.id]: [choice] })), {});
    }
    onChoicesUpdate(choices) {
        this.updateComposedChoices(choices, this.choiceMap);
    }
    onChoiceMapUpdate(choiceMap) {
        this.updateComposedChoices(this.remoteChoices, choiceMap);
    }
    updateComposedChoices(searchChoices, choiceMap) {
        this._selectedChoices = choiceMap && choiceMap[this.choiceId] ? choiceMap[this.choiceId] : [];
        this._searchChoices = searchChoices;
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-business-classification-selector-container", { answerId: this.answerId, choiceId: this.choiceId, choices: this.choices, code: this.code, config: this.config, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, labelId: this.labelId, label: this.label, labelledBy: this.labelledBy, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, remoteChoices: this.remoteChoices, required: this.required, touched: this.touched, showConfidence: this.showConfidence, showNaicsCode: this.showNaicsCode, selectedChoices: this._selectedChoices }));
    }
    renderInternal() {
        return [
            h("bp-sdk-business-classification-selector", { answerId: this.answerId, choiceId: this.choiceId, choices: this._searchChoices, code: this.code, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, label: this.label, labelId: this.labelId, labelledBy: this.labelledBy, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, required: this.required, touched: this.touched, showChoices: this.showChoices, showConfidence: this.showConfidence, showNaicsCode: this.showNaicsCode, selectedChoices: this._selectedChoices }),
        ];
    }
    static get is() { return "bp-sdk-business-classification-selector-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-business-classification-selector-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-business-classification-selector-container.css"]
        };
    }
    static get properties() {
        return {
            "answerId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Answer id"
                },
                "attribute": "answer-id",
                "reflect": false
            },
            "questionId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question id"
                },
                "attribute": "question_id",
                "reflect": false
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question code"
                },
                "attribute": "code",
                "reflect": false,
                "defaultValue": "''"
            },
            "placeholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Placeholder to display"
                },
                "attribute": "placeholder",
                "reflect": false,
                "defaultValue": "''"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is required value"
                },
                "attribute": "required",
                "reflect": false,
                "defaultValue": "false"
            },
            "errors": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "string[] | undefined",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error messages"
                }
            },
            "errorText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error text"
                },
                "attribute": "error_text",
                "reflect": false
            },
            "labelId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Aria label id's that describes input for screen readers"
                },
                "attribute": "label-id",
                "reflect": false
            },
            "choiceId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "*\nSelected choice"
                },
                "attribute": "choice_id",
                "reflect": false
            },
            "touched": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is touched"
                },
                "attribute": "touched",
                "reflect": false,
                "defaultValue": "false"
            },
            "pristine": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is pristine"
                },
                "attribute": "pristine",
                "reflect": false,
                "defaultValue": "true"
            },
            "isCompleted": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is answer in complete state"
                },
                "attribute": "is_completed",
                "reflect": false,
                "defaultValue": "false"
            },
            "isReadOnly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "When true, the field will be readonly. It will display only the label and value as plain text."
                },
                "attribute": "is-read-only",
                "reflect": false,
                "defaultValue": "false"
            },
            "fieldType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Field type"
                },
                "attribute": "field_type",
                "reflect": false
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label to use"
                },
                "attribute": "label",
                "reflect": false
            },
            "labelledBy": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Labels that describe group"
                },
                "attribute": "labelled-by",
                "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": "Choices from answer"
                },
                "defaultValue": "[]"
            },
            "remoteChoices": {
                "type": "unknown",
                "mutable": true,
                "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": "Choices injected from redux"
                },
                "defaultValue": "[]"
            },
            "showNaicsCode": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Show naics code preference injected from redux"
                },
                "attribute": "show-naics-code",
                "reflect": false,
                "defaultValue": "true"
            },
            "showConfidence": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Show confidence score preference injected from redux"
                },
                "attribute": "show-confidence",
                "reflect": false,
                "defaultValue": "true"
            },
            "config": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IPartnerEngineConfig",
                    "resolved": "IPartnerEngineConfig",
                    "references": {
                        "IPartnerEngineConfig": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IPartnerEngineConfig"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "config injected from redux"
                }
            },
            "helpText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Help text"
                },
                "attribute": "help-text",
                "reflect": false
            },
            "answeredBySource": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Specifically how the question was answered (ie. \"ACORD 126\")"
                },
                "attribute": "answered-by-source",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "choiceMap": {},
            "_selectedChoices": {},
            "_searchChoices": {},
            "_currentSearchValue": {},
            "useExternal": {},
            "showChoices": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "questionId",
                "methodName": "onQuestionIdChange"
            }, {
                "propName": "choices",
                "methodName": "onChoicesChange"
            }, {
                "propName": "remoteChoices",
                "methodName": "onChoicesUpdate"
            }, {
                "propName": "choiceMap",
                "methodName": "onChoiceMapUpdate"
            }];
    }
    static get listeners() {
        return [{
                "name": "filterUpdate",
                "method": "onFilterUpdate",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "nudgeAnswer",
                "method": "onRefilterAnswer",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "valueUpdate",
                "method": "onValueUpdate",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
