import { QuestionFieldType, QuestionGroupType, } from "@boldpenguin/sdk-types";
import { generateId } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
import { getCurrentActiveQuestionSet } from "@boldpenguin/sdk-redux";
import store from "../../services/redux-store.service";
export class BpSdkQuestionGroup {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            var _a, _b;
            return {
                questionPools: (_b = (_a = getCurrentActiveQuestionSet(state)) === null || _a === void 0 ? void 0 : _a.question_set) === null || _b === void 0 ? void 0 : _b.question_pools,
            };
        };
        this.answers = [];
        this.classes = '';
        this.label = undefined;
        this.labelId = generateId(8);
        this.questionGroup = undefined;
        this.type = undefined;
        this.questionPools = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-question-group');
        customElements.whenDefined('bp-sdk-external-question-group').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderUnknownGroupType(answer) {
        var _a;
        const nestedGroupToRender = (_a = this.questionGroup.nestedGroupsWithAnswers) === null || _a === void 0 ? void 0 : _a.get(answer.id);
        return [
            !nestedGroupToRender && (h("bp-sdk-dynamic-input-redux-container", { answer: answer, labelledBy: this.getLabelledBy(), id: `bp-sdk-question-group__${answer.question.id}`, class: "bp-sdk-question-group__question-set" })),
            nestedGroupToRender && h("bp-sdk-question-group-container", { questionGroup: nestedGroupToRender }),
        ];
    }
    renderExternal() {
        return (h("bp-sdk-external-question-group", { answers: this.answers, classes: this.classes, label: this.label, labelId: this.labelId, questionGroup: this.questionGroup, questionPools: this.questionPools, type: this.type }));
    }
    hideNestedGroup(group) {
        const shouldHide = [];
        group.forEach((child) => {
            var _a;
            const onlyAnchors = (_a = child.answers) === null || _a === void 0 ? void 0 : _a.every((a) => a.question.field_type === QuestionFieldType.Anchor);
            if (onlyAnchors && child.nestedGroupsWithAnswers.size > 0) {
                shouldHide.push(this.hideNestedGroup(child.nestedGroupsWithAnswers));
            }
            else {
                shouldHide.push(onlyAnchors || child.answers.length === 0);
            }
        });
        return shouldHide.every(Boolean);
    }
    shouldRenderLabel() {
        // Adding a check to avoid accordion and coverage groups double rendering labels
        // this can likely be expanded to other group types
        return (this.label &&
            this.type !== QuestionGroupType.Accordion &&
            this.type !== QuestionGroupType.Coverage &&
            this.type !== QuestionGroupType.AutoCollapsedAccordion);
    }
    hideQuestionGroup() {
        var _a, _b, _c;
        const onlyAnchors = (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.answers) === null || _b === void 0 ? void 0 : _b.every((a) => a.question.field_type === QuestionFieldType.Anchor);
        if (onlyAnchors) {
            // If an anchor is a parent of a nested group.
            if (((_c = this.questionGroup.nestedGroupsWithAnswers) === null || _c === void 0 ? void 0 : _c.size) > 0) {
                return this.hideNestedGroup(this.questionGroup.nestedGroupsWithAnswers);
            }
            // If an anchor is part of a question pool, render it.
            return (this.questionGroup.answers.find((a) => { var _a; return !!((_a = this.questionPools) === null || _a === void 0 ? void 0 : _a.find((p) => p.parent_question_id === a.question.id)); }) === undefined);
        }
        return false;
    }
    getLabelledBy() {
        return this.shouldRenderLabel() ? this.labelId : '';
    }
    renderInternal() {
        if (this.hideQuestionGroup()) {
            return;
        }
        return [
            this.shouldRenderLabel() && h("bp-sdk-question-group-label", { labelId: this.labelId, label: this.label }),
            h("div", { class: this.classes }, this.renderContent()),
        ];
    }
    renderContent() {
        switch (this.type) {
            case QuestionGroupType.Address:
                return (h("bp-sdk-address-group-container", { questionGroup: this.questionGroup, label: this.label, labelledBy: this.getLabelledBy(), answers: this.answers }));
            case QuestionGroupType.AccumulatePercentage:
                return h("bp-sdk-accumulation-question-group-container", { label: this.label, labelledBy: this.getLabelledBy(), answers: this.answers });
            case QuestionGroupType.Vehicle:
                return h("bp-sdk-vehicle-question-pool-container", { questionGroup: this.questionGroup });
            case QuestionGroupType.Driver:
                return h("bp-sdk-driver-question-pool-container", { questionGroup: this.questionGroup });
            case QuestionGroupType.Stepper:
                return h("bp-sdk-stepper-group-container", { questionGroup: this.questionGroup });
            case QuestionGroupType.Accordion:
                return h("bp-sdk-accordion", { questionGroup: this.questionGroup, startPanelExpanded: true });
            case QuestionGroupType.Coverage:
                return h("bp-sdk-coverage-group", { questionGroup: this.questionGroup });
            case QuestionGroupType.AutoCollapsedAccordion:
                return h("bp-sdk-accordion", { questionGroup: this.questionGroup, startPanelExpanded: false });
            default:
                return this.answers.map((answer) => this.renderUnknownGroupType(answer));
        }
    }
    static get is() { return "bp-sdk-question-group"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-question-group.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-question-group.css"]
        };
    }
    static get properties() {
        return {
            "answers": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IAnswer[]",
                    "resolved": "IAnswer[]",
                    "references": {
                        "IAnswer": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAnswer"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "answers in group"
                },
                "defaultValue": "[]"
            },
            "classes": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Classes for styling group"
                },
                "attribute": "classes",
                "reflect": false,
                "defaultValue": "''"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Group label"
                },
                "attribute": "label",
                "reflect": false
            },
            "labelId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label id's for group"
                },
                "attribute": "label-id",
                "reflect": false,
                "defaultValue": "generateId(8)"
            },
            "questionGroup": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IMappedQuestionGroupAnswers",
                    "resolved": "IMappedQuestionGroupAnswers",
                    "references": {
                        "IMappedQuestionGroupAnswers": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IMappedQuestionGroupAnswers"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question Group"
                }
            },
            "type": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "group type"
                },
                "attribute": "type",
                "reflect": true
            },
            "questionPools": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IQuestionPool[]",
                    "resolved": "IQuestionPool[]",
                    "references": {
                        "IQuestionPool": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuestionPool"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {}
        };
    }
}
