import { r as registerInstance, h } from './index-77a9c9e7.js';
import { a as an, S as S5, _ as _8, m as m8, O as Oe, u as up, b as me, c as cR, l as lR, d as dR, f as fR } from './index.esm.min-db2d29e8.js';
import { d } from './index.esm.min-9889e78b.js';
import { s as store } from './redux-store.service-dc54f48e.js';

const bpSdkBackButtonContainerCss = "";
const BpSdkBackButtonContainerStyle0 = bpSdkBackButtonContainerCss;

const BpSdkBackButtonContainer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.handleSubscriptions = () => {
            if (this.storeUnsubscribe) {
                this.storeUnsubscribe();
            }
            store.mapDispatchToProps(this, {
                saveQuestionSet: an,
                updateErrors: S5,
                removeActiveAnswerId: _8,
                switchActiveQuestionSet: m8,
            });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSet: Oe(state),
                config: up(state),
                questionSets: me(state),
                queue: state.queues.answers.queue,
                answersResourceState: cR(state),
                applicationFormResourceState: lR(state),
                questionPoolResourceState: dR(state),
                questionSetResourceState: fR(state),
            };
        };
        this.activeQuestionSet = undefined;
        this.config = undefined;
        this.buttonDisabled = false;
        this.questionSets = [];
        this.queue = undefined;
        this.answersResourceState = undefined;
        this.applicationFormResourceState = undefined;
        this.questionPoolResourceState = undefined;
        this.questionSetResourceState = undefined;
        this.shouldHideForFirstQuestionSet = false;
        this.useExternal = false;
        this.canSave = true;
        this.areResourcesReady = true;
        this.isReady = true;
        this.isFirstRun = true;
        this.isFirstQnSet = true;
        this.isDisabled = false;
    }
    resetState() { }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-back-button-container');
        customElements.whenDefined('bp-sdk-external-back-button-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.onQuestionSetsChange(this.questionSets);
        this.onActiveQuestionSetChange(this.activeQuestionSet);
        this.onIsReady(this.isReady);
        this.onAnswersResourcesStateChange(this.answersResourceState);
        this.onApplicationFormResourceStateChange(this.applicationFormResourceState);
        this.onIsFirstRun(this.isFirstRun);
        this.onIsReady(this.isReady);
        this.onQuestionPoolResourcesStateChange(this.questionPoolResourceState);
        this.onQuestionSetResourcesStateChange(this.questionSetResourceState);
        this.onQueueChange(this.queue);
    }
    onIsReady(isReady) {
        this.updateAreResourcesReady(this.isFirstRun, isReady);
    }
    updateAreResourcesReady(isFirstRun, isReady) {
        this.areResourcesReady = isFirstRun || isReady;
    }
    onAnswersResourcesStateChange(state) {
        this.isReady = this.calculateResourcesReady({
            answers: state,
            applicationForm: this.applicationFormResourceState,
            questionPools: this.questionPoolResourceState,
            questionSets: this.questionSetResourceState,
        });
    }
    onApplicationFormResourceStateChange(state) {
        if (this.isFirstRun && state === d.Loaded) {
            this.isFirstRun = false;
        }
        this.isReady = this.calculateResourcesReady({
            answers: this.answersResourceState,
            applicationForm: state,
            questionPools: this.questionPoolResourceState,
            questionSets: this.questionSetResourceState,
        });
    }
    onQuestionPoolResourcesStateChange(state) {
        this.isReady = this.calculateResourcesReady({
            answers: this.answersResourceState,
            applicationForm: this.applicationFormResourceState,
            questionPools: state,
            questionSets: this.questionSetResourceState,
        });
    }
    onQuestionSetResourcesStateChange(state) {
        this.isReady = this.calculateResourcesReady({
            answers: this.answersResourceState,
            applicationForm: this.applicationFormResourceState,
            questionPools: this.questionPoolResourceState,
            questionSets: state,
        });
    }
    calculateResourcesReady(resources = {}) {
        if (!resources ||
            resources.applicationForm !== d.Loaded ||
            resources.answers === d.Loading ||
            resources.questionPools === d.Loading ||
            resources.questionSets === d.Loading) {
            return false;
        }
        return true;
    }
    onActiveQuestionSetChange(activeQuestionSet) {
        this.updateIsFirstQnSet(this.questionSets, activeQuestionSet);
    }
    onIsFirstRun(isFirstRun) {
        this.updateAreResourcesReady(isFirstRun, this.isReady);
    }
    onQuestionSetsChange(qns) {
        this.updateIsFirstQnSet(qns, this.activeQuestionSet);
    }
    updateIsFirstQnSet(qns, activeQns) {
        var _a;
        this.isFirstQnSet = ((_a = qns[0]) === null || _a === void 0 ? void 0 : _a.question_set_id) === (activeQns === null || activeQns === void 0 ? void 0 : activeQns.question_set_id);
    }
    onQueueChange(queue) {
        this.canSave = !(queue === null || queue === void 0 ? void 0 : queue.length);
    }
    shouldRender() {
        return this.isFirstQnSet ? !this.shouldHideForFirstQuestionSet : true;
    }
    render() {
        if (this.shouldRender()) {
            return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
        }
    }
    renderExternal() {
        return (h("bp-sdk-external-back-button-container", { activeQuestionSet: this.activeQuestionSet, answersResourceState: this.answersResourceState, applicationFormResourceState: this.applicationFormResourceState, buttonDisabled: this.buttonDisabled, config: this.config, questionPoolResourceState: this.questionPoolResourceState, questionSetResourceState: this.questionSetResourceState, questionSets: this.questionSets, queue: this.queue }));
    }
    handleNavigateBack() {
        this.switchActiveQuestionSet('backward');
    }
    renderInternal() {
        return (h("bp-sdk-back-button", { buttonDisabled: this.buttonDisabled || !this.areResourcesReady || !this.canSave || this.isFirstQnSet, cssClasses: !this.areResourcesReady || !this.canSave ? 'loading' : 'not-loading' }));
    }
    static get watchers() { return {
        "isReady": ["onIsReady"],
        "answersResourceState": ["onAnswersResourcesStateChange"],
        "applicationFormResourceState": ["onApplicationFormResourceStateChange"],
        "questionPoolResourceState": ["onQuestionPoolResourcesStateChange"],
        "questionSetResourceState": ["onQuestionSetResourcesStateChange"],
        "activeQuestionSet": ["onActiveQuestionSetChange"],
        "isFirstRun": ["onIsFirstRun"],
        "questionSets": ["onQuestionSetsChange"],
        "queue": ["onQueueChange"]
    }; }
};
BpSdkBackButtonContainer.style = BpSdkBackButtonContainerStyle0;

export { BpSdkBackButtonContainer as bp_sdk_back_button_container };
