'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 index_esm_min$1 = require('./index.esm.min-1101deb1.js');
const reduxStore_service = require('./redux-store.service-10b7725a.js');

const bpSdkBackButtonContainerCss = "";
const BpSdkBackButtonContainerStyle0 = bpSdkBackButtonContainerCss;

const BpSdkBackButtonContainer = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.handleSubscriptions = () => {
            if (this.storeUnsubscribe) {
                this.storeUnsubscribe();
            }
            reduxStore_service.store.mapDispatchToProps(this, {
                saveQuestionSet: index_esm_min.an,
                updateErrors: index_esm_min.S5,
                removeActiveAnswerId: index_esm_min._8,
                switchActiveQuestionSet: index_esm_min.m8,
            });
            this.storeUnsubscribe = reduxStore_service.store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSet: index_esm_min.Oe(state),
                config: index_esm_min.up(state),
                questionSets: index_esm_min.me(state),
                queue: state.queues.answers.queue,
                answersResourceState: index_esm_min.cR(state),
                applicationFormResourceState: index_esm_min.lR(state),
                questionPoolResourceState: index_esm_min.dR(state),
                questionSetResourceState: index_esm_min.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 === index_esm_min$1.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 !== index_esm_min$1.d.Loaded ||
            resources.answers === index_esm_min$1.d.Loading ||
            resources.questionPools === index_esm_min$1.d.Loading ||
            resources.questionSets === index_esm_min$1.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 (index.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 (index.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;

exports.bp_sdk_back_button_container = BpSdkBackButtonContainer;
