'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-ceff47a0.js');
const index_esm_min = require('./index.esm.min-f57d151c.js');
const reduxStore_service = require('./redux-store.service-10b7725a.js');

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

exports.bp_sdk_question_set_navigation_container = BpSdkQuestionSetNavigationContainer;
