import { r as registerInstance, h, g as getElement } from './index-77a9c9e7.js';
import { n as b8, _ as _8, P as Pi, p as aN, q as oN, r as mL, b as me } from './index.esm.min-db2d29e8.js';
import { s as store } from './redux-store.service-dc54f48e.js';

const BpSdkQuestionSetNavigationContainer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { updateActiveQuestionSet: b8, removeActiveAnswerId: _8 });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSetId: Pi(state),
                areParentQuestionSetsInNavigationEnabled: aN(state),
                isQuestionSetNavigationEnabled: oN(state),
                parentQuestionSets: mL(state),
                questionSetsList: me(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 }));
    }
    get el() { return getElement(this); }
    static get watchers() { return {
        "questionSetsList": ["onQuestionSetPropsChange"],
        "parentQuestionSets": ["onQuestionSetPropsChange"]
    }; }
};

export { BpSdkQuestionSetNavigationContainer as bp_sdk_question_set_navigation_container };
