import { addQuestionPool, getCurrentActiveQuestionSet, getFormId, getPartnerEngineConfig } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkQuestionPoolContainer {
    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),
                currentQuestionSet: getCurrentActiveQuestionSet(state),
            };
        };
        this.handleAddItem = () => {
            if (!!this.parentQuestionPoolReferenceId && !this.selectParentPoolQuestion) {
                this.selectParentPoolQuestion = true;
            }
            else if (!this.parentQuestionPoolReferenceId) {
                this.addQuestionPool({
                    config: this.config,
                    application_id: this.applicationId,
                    question_pool_id: this.guid,
                    parent_question_pool_id: this.parentQuestionPoolId,
                    selected_answer_pool_id: !!this.parentQuestionPoolId ? this.answerPoolId : undefined,
                });
            }
        };
        this.addText = undefined;
        this.answerPoolId = undefined;
        this.code = undefined;
        this.description = undefined;
        this.displayText = undefined;
        this.guid = undefined;
        this.parentQuestionPoolId = undefined;
        this.parentQuestionPoolReferenceId = undefined;
        this.ownerId = undefined;
        this.parentQuestionId = undefined;
        this.removeText = undefined;
        this.tenantId = undefined;
        this.questionId = undefined;
        this.questionSetId = undefined;
        this.config = undefined;
        this.applicationId = undefined;
        this.currentQuestionSet = undefined;
        this.useExternal = false;
        this.selectParentPoolQuestion = false;
    }
    onParentQuestionPoolSelected(event) {
        this.addQuestionPool({
            config: this.config,
            application_id: this.applicationId,
            question_pool_id: this.guid,
            parent_question_pool_id: this.parentQuestionPoolId,
            selected_answer_pool_id: event.detail,
        });
    }
    resetState() { }
    connectedCallback() {
        this.handleSubscriptions();
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-question-pool-container');
        customElements.whenDefined('bp-sdk-external-question-pool-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    getParentAnswers() {
        return this.currentQuestionSet.answers.filter((answer) => answer.question.id === this.parentQuestionPoolReferenceId);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-question-pool-container", { addText: this.addText, code: this.code, description: this.description, displayText: this.displayText, guid: this.guid, ownerId: this.ownerId, parentQuestionId: this.parentQuestionId, parentQuestionPoolId: this.parentQuestionPoolId, parentQuestionPoolReferenceId: this.parentQuestionPoolReferenceId, removeText: this.removeText, tenantId: this.tenantId, config: this.config, applicationId: this.applicationId, currentQuestionSet: this.currentQuestionSet, questionId: this.questionId, questionSetId: this.questionSetId }));
    }
    renderAnswer() {
        const choices = this.getParentAnswers().map((a, i) => ({
            alias: a.question.text,
            created_at: '',
            updated_at: '',
            id: a.answer_pool_id,
            order: i,
            question_id: a.question.id,
            value: a.value,
        }));
        return h("bp-sdk-parent-question-pool-selector", { choices: choices, label: choices[0].alias });
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-question-pool-container__container" }, h("bp-sdk-add-item-container", { answerPoolId: this.answerPoolId, text: this.addText, helpText: this.displayText, onAddItem: this.handleAddItem, questionId: this.questionId, questionSetId: this.questionSetId }, h("slot", null)), this.selectParentPoolQuestion && this.renderAnswer()));
    }
    static get is() { return "bp-sdk-question-pool-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-question-pool-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-question-pool-container.css"]
        };
    }
    static get properties() {
        return {
            "addText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Add button text"
                },
                "attribute": "add-text",
                "reflect": false
            },
            "answerPoolId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Answer pool id"
                },
                "attribute": "answer-pool-id",
                "reflect": false
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool code"
                },
                "attribute": "code",
                "reflect": false
            },
            "description": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool description"
                },
                "attribute": "description",
                "reflect": false
            },
            "displayText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool display text"
                },
                "attribute": "display-text",
                "reflect": false
            },
            "guid": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool guid"
                },
                "attribute": "guid",
                "reflect": false
            },
            "parentQuestionPoolId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | null",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Parent question pool id"
                },
                "attribute": "parent-question-pool-id",
                "reflect": false
            },
            "parentQuestionPoolReferenceId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | null",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Parent question pool reference id"
                },
                "attribute": "parent-question-pool-reference-id",
                "reflect": false
            },
            "ownerId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool owner id"
                },
                "attribute": "owner-id",
                "reflect": false
            },
            "parentQuestionId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Anchor question id"
                },
                "attribute": "parent-question-id",
                "reflect": false
            },
            "removeText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Remove button text"
                },
                "attribute": "remove-text",
                "reflect": false
            },
            "tenantId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "tenant id for question pool"
                },
                "attribute": "tenant-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
            },
            "questionSetId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question Id"
                },
                "attribute": "question-set-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": ""
                }
            },
            "applicationId": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "application-id",
                "reflect": false
            },
            "currentQuestionSet": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IApplicationFormQuestionSet",
                    "resolved": "IApplicationFormQuestionSet",
                    "references": {
                        "IApplicationFormQuestionSet": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IApplicationFormQuestionSet"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "selectParentPoolQuestion": {}
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "selectedAnswerPoolId",
                "method": "onParentQuestionPoolSelected",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
