import { getPartnerEngineConfig, getSelectedChoices, searchChoices } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import uniqBy from "lodash/uniqBy";
import store from "../../services/redux-store.service";
export class BpSdkAutocompleteNaicsContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { searchChoices });
            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.placeholder = '';
        this.required = false;
        this.code = undefined;
        this.errors = undefined;
        this.errorText = undefined;
        this.labelId = undefined;
        this.choiceId = undefined;
        this.choices = [];
        this.touched = false;
        this.pristine = true;
        this.isCompleted = false;
        this.isReadOnly = false;
        this.fieldType = undefined;
        this.label = undefined;
        this.labelledBy = undefined;
        this.remoteChoices = [];
        this.config = undefined;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this.choiceMap = {};
        this.composedChoices = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-autocomplete-naics-container');
        customElements.whenDefined('bp-sdk-external-autocomplete-naics-container').then(() => {
            this.useExternal = true;
        });
        this.onQuestionIdChange();
        this.onChoicesChange(this.choices || []);
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    onQuestionIdChange() {
        this.handleSubscriptions();
    }
    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) {
        var _a;
        const answerChoices = (_a = (choiceMap && choiceMap[this.choiceId])) !== null && _a !== void 0 ? _a : [];
        this.composedChoices = uniqBy([...searchChoices, ...answerChoices], (item) => item.id);
    }
    onFilterUpdate({ detail }) {
        if (this.useExternal) {
            return;
        }
        const params = {
            questionId: this.questionId,
            filter: detail.value,
            timestamp: Date.now(),
            config: this.config,
        };
        this.searchChoices(params);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-autocomplete-naics-container", { "answer-id": this.answerId, choice_id: this.choiceId, choices: this.choices, code: this.code, config: this.config, errors: this.errors, error_text: this.errorText, field_type: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, is_completed: this.isCompleted, isReadOnly: this.isReadOnly, "label-id": this.labelId, label: this.label, "labelled-by": this.labelledBy, placeholder: this.placeholder, pristine: this.pristine, question_id: this.questionId, remoteChoices: this.remoteChoices, required: this.required, touched: this.touched }));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-autocomplete-naics-container__container" }, h("bp-sdk-autocomplete-input", { answeredBySource: this.answeredBySource, answerId: this.answerId, choiceId: this.choiceId, choices: this.composedChoices, code: this.code, errors: this.errors, errorText: this.errorText, helpText: this.helpText, fieldType: this.fieldType, 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 })));
    }
    static get is() { return "bp-sdk-autocomplete-naics-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-autocomplete-naics-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-autocomplete-naics-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
            },
            "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"
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "question code"
                },
                "attribute": "code",
                "reflect": 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
            },
            "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": "[]"
            },
            "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
            },
            "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": "[]"
            },
            "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": "IPartnerEngineConfig 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": {},
            "composedChoices": {},
            "useExternal": {}
        };
    }
    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
            }];
    }
}
