import { addQuestionPool, getFormId, getPartnerEngineConfig, selectQuestionSetsState } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkAddQuestionPoolItemContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { addQuestionPool });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                applicationId: getFormId(state),
                config: getPartnerEngineConfig(state),
                questionSetState: selectQuestionSetsState(state),
            };
        };
        this.answerPoolId = undefined;
        this.questionId = undefined;
        this.helpText = '';
        this.questionSetId = undefined;
        this.text = '+';
        this.applicationId = undefined;
        this.config = undefined;
        this.questionSetState = undefined;
        this.useExternal = false;
        this.questionPool = undefined;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-add-question-pool-item-container');
        customElements.whenDefined('bp-sdk-external-add-question-pool-item-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.onQuestionSetIdChange(this.questionSetId);
        this.onQuestionIdChange(this.questionId);
    }
    onQuestionSetIdChange(questionSetId) {
        this.updateQuestionPool(questionSetId, this.questionId, this.questionSetState);
    }
    onQuestionIdChange(questionId) {
        this.updateQuestionPool(this.questionSetId, questionId, this.questionSetState);
    }
    onQuestionSetsChange(questionSetState) {
        this.updateQuestionPool(this.questionSetId, this.questionId, questionSetState);
    }
    updateQuestionPool(questionSetId, questionId, questionSetState) {
        var _a, _b, _c;
        let set = (_a = questionSetState === null || questionSetState === void 0 ? void 0 : questionSetState.list) === null || _a === void 0 ? void 0 : _a.filter((set) => set.question_set_id === questionSetId)[0];
        const f = (questionSetState === null || questionSetState === void 0 ? void 0 : questionSetState.parentQuestionSets.length) > 0;
        if (f) {
            const hasParent = questionSetState.parentQuestionSets.find((qs) => qs.question_set_id === questionSetId);
            if (hasParent) {
                set = hasParent;
            }
        }
        const pools = (_c = (_b = set === null || set === void 0 ? void 0 : set.question_set) === null || _b === void 0 ? void 0 : _b.question_pools) !== null && _c !== void 0 ? _c : [];
        this.questionPool = pools.find((pool) => pool.parent_question_id === questionId);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-add-question-pool-item-container", { answerPoolId: this.answerPoolId, applicationId: this.applicationId, config: this.config, helpText: this.helpText, questionId: this.questionId, questionSetId: this.questionSetId, questionSetState: this.questionSetState, text: this.text }));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-add-question-pool-item-container__container" }, this.questionPool && (h("bp-sdk-question-pool-container", { addText: this.questionPool.add_text, answerPoolId: this.answerPoolId, code: this.questionPool.code, description: this.questionPool.description, displayText: this.questionPool.display_text, guid: this.questionPool.id, parentQuestionPoolId: this.questionPool.parent_question_pool_id, parentQuestionPoolReferenceId: this.questionPool.parent_question_pool_reference_id, ownerId: this.questionPool.owner_id, parentQuestionId: this.questionPool.parent_question_id, removeText: this.questionPool.remove_text, tenantId: this.questionPool.tenant_id, config: this.config, applicationId: this.applicationId, questionId: this.questionId, questionSetId: this.questionSetId }))));
    }
    static get is() { return "bp-sdk-add-question-pool-item-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-add-question-pool-item-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-add-question-pool-item-container.css"]
        };
    }
    static get properties() {
        return {
            "answerPoolId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The id that the answer pool the answer belongs to.\nIf undefined, then not part of a pool."
                },
                "attribute": "answer-pool-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
            },
            "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,
                "defaultValue": "''"
            },
            "questionSetId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question set id"
                },
                "attribute": "question-set-id",
                "reflect": false
            },
            "text": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Add button text"
                },
                "attribute": "text",
                "reflect": false,
                "defaultValue": "'+'"
            },
            "applicationId": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "application-id",
                "reflect": false
            },
            "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": ""
                }
            },
            "questionSetState": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IQuestionSetState",
                    "resolved": "IQuestionSetState",
                    "references": {
                        "IQuestionSetState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuestionSetState"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "questionPool": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "questionSetId",
                "methodName": "onQuestionSetIdChange"
            }, {
                "propName": "questionId",
                "methodName": "onQuestionIdChange"
            }, {
                "propName": "questionSetState",
                "methodName": "onQuestionSetsChange"
            }];
    }
}
