import { getCurrentActiveQuestionSetId, getQuestionSets, removeActiveAnswerId, selectAreParentQuestionSetsInNavigationEnabled, selectIsQuestionSetNavigationEnabled, selectParentQuestionSets, updateActiveQuestionSet, } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkQuestionSetNavigationContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { updateActiveQuestionSet, removeActiveAnswerId });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSetId: getCurrentActiveQuestionSetId(state),
                areParentQuestionSetsInNavigationEnabled: selectAreParentQuestionSetsInNavigationEnabled(state),
                isQuestionSetNavigationEnabled: selectIsQuestionSetNavigationEnabled(state),
                parentQuestionSets: selectParentQuestionSets(state),
                questionSetsList: getQuestionSets(state),
            };
        };
        this.activeQuestionSetId = undefined;
        this.areParentQuestionSetsInNavigationEnabled = undefined;
        this.isQuestionSetNavigationEnabled = undefined;
        this.parentQuestionSets = [];
        this.questionSetsList = [];
        this.useExternal = false;
        this.questionSets = [];
    }
    resetState() {
        this.questionSets = [];
    }
    onQuestionSetPropsChange() {
        const insertedParentQuestionSets = new WeakMap();
        if (this.areParentQuestionSetsInNavigationEnabled) {
            this.questionSets = this.questionSetsList.reduce((acc, set) => {
                if (set.parentQuestionSetId) {
                    const parentQuestionSet = this.parentQuestionSets.find((parent) => parent.id === set.parentQuestionSetId);
                    if (parentQuestionSet && !insertedParentQuestionSets.has(parentQuestionSet)) {
                        insertedParentQuestionSets.set(parentQuestionSet, true);
                        // Navigation is based on `question_set_id` so we need to override it on the parent question set
                        // so that it will navigate to the first child question set when clicked
                        acc.push(Object.assign(Object.assign({}, parentQuestionSet), { question_set_id: set.question_set_id, isParent: true }));
                    }
                    acc.push(Object.assign(Object.assign({}, set), { isChild: true }));
                }
                else {
                    acc.push(set);
                }
                return acc;
            }, []);
        }
        else {
            this.questionSets = this.questionSetsList;
        }
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-question-set-navigation-container');
        customElements.whenDefined('bp-sdk-external-question-set-navigation-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.onQuestionSetPropsChange();
    }
    onRemoveActiveAnswerRequest() {
        this.removeActiveAnswerId();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    handleActiveQuestionSetUpdate(evt) {
        if (this.useExternal) {
            return;
        }
        evt.detail && this.updateActiveQuestionSet(evt.detail);
        this.removeActiveAnswerId();
    }
    componentWillUpdate() {
        this.handleSubscriptions();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-question-set-navigation-container", { activeQuestionSetId: this.activeQuestionSetId, areParentQuestionSetsInNavigationEnabled: this.areParentQuestionSetsInNavigationEnabled, isQuestionSetNavigationEnabled: this.isQuestionSetNavigationEnabled, parentQuestionSets: this.parentQuestionSets, questionSets: this.questionSets, questionSetsList: this.questionSetsList }));
    }
    renderInternal() {
        return (h("bp-sdk-question-set-navigation", { activeId: this.activeQuestionSetId, questionSets: this.questionSets, isNavigationEnabled: this.isQuestionSetNavigationEnabled }));
    }
    static get is() { return "bp-sdk-question-set-navigation-container"; }
    static get properties() {
        return {
            "activeQuestionSetId": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Active question set ID injected by redux"
                },
                "attribute": "active-question-set-id",
                "reflect": false
            },
            "areParentQuestionSetsInNavigationEnabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Config: Parent question set navigation enabled? Injected by redux"
                },
                "attribute": "are-parent-question-sets-in-navigation-enabled",
                "reflect": false
            },
            "isQuestionSetNavigationEnabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Config: Question set navigation enabled? Injected by redux"
                },
                "attribute": "is-question-set-navigation-enabled",
                "reflect": false
            },
            "parentQuestionSets": {
                "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": "Parent question sets injected from redux"
                },
                "defaultValue": "[]"
            },
            "questionSetsList": {
                "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": "Question sets list injected by redux"
                },
                "defaultValue": "[]"
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "questionSets": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "questionSetsList",
                "methodName": "onQuestionSetPropsChange"
            }, {
                "propName": "parentQuestionSets",
                "methodName": "onQuestionSetPropsChange"
            }];
    }
    static get listeners() {
        return [{
                "name": "removeActiveAnswer",
                "method": "onRemoveActiveAnswerRequest",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "activeIdUpdate",
                "method": "handleActiveQuestionSetUpdate",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
