import { getAnswersState, getApplicationFormState, getCurrentActiveQuestionSet, getPartnerEngineConfig, getQuestionPoolState, getQuestionSetState, getQuestionSets, removeActiveAnswerId, saveQuestionSet, switchActiveQuestionSet, updateErrors, } from "@boldpenguin/sdk-redux";
import { LoadingStates, } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkBackButtonContainer {
    constructor() {
        this.handleSubscriptions = () => {
            if (this.storeUnsubscribe) {
                this.storeUnsubscribe();
            }
            store.mapDispatchToProps(this, {
                saveQuestionSet,
                updateErrors,
                removeActiveAnswerId,
                switchActiveQuestionSet,
            });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSet: getCurrentActiveQuestionSet(state),
                config: getPartnerEngineConfig(state),
                questionSets: getQuestionSets(state),
                queue: state.queues.answers.queue,
                answersResourceState: getAnswersState(state),
                applicationFormResourceState: getApplicationFormState(state),
                questionPoolResourceState: getQuestionPoolState(state),
                questionSetResourceState: getQuestionSetState(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 === LoadingStates.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 !== LoadingStates.Loaded ||
            resources.answers === LoadingStates.Loading ||
            resources.questionPools === LoadingStates.Loading ||
            resources.questionSets === LoadingStates.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 is() { return "bp-sdk-back-button-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-back-button-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-back-button-container.css"]
        };
    }
    static get properties() {
        return {
            "activeQuestionSet": {
                "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": ""
                }
            },
            "config": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IPartnerEngineConfig",
                    "resolved": "IPartnerEngineConfig",
                    "references": {
                        "IPartnerEngineConfig": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IPartnerEngineConfig"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "IPartnerEngineConfig injected from redux"
                }
            },
            "buttonDisabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Controls disabled state of button"
                },
                "attribute": "button-disabled",
                "reflect": false,
                "defaultValue": "false"
            },
            "questionSets": {
                "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"
                },
                "defaultValue": "[]"
            },
            "queue": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IAnswer[]",
                    "resolved": "IAnswer[]",
                    "references": {
                        "IAnswer": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAnswer"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Answer queue"
                }
            },
            "answersResourceState": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "LoadingStates",
                    "resolved": "LoadingStates.Error | LoadingStates.Loaded | LoadingStates.Loading | LoadingStates.NotLoaded",
                    "references": {
                        "LoadingStates": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::LoadingStates"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Answer resource states injected from redux"
                },
                "attribute": "answers-resource-state",
                "reflect": false
            },
            "applicationFormResourceState": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "LoadingStates",
                    "resolved": "LoadingStates.Error | LoadingStates.Loaded | LoadingStates.Loading | LoadingStates.NotLoaded",
                    "references": {
                        "LoadingStates": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::LoadingStates"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Application form resource states injected from redux"
                },
                "attribute": "application-form-resource-state",
                "reflect": false
            },
            "questionPoolResourceState": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "LoadingStates",
                    "resolved": "LoadingStates.Error | LoadingStates.Loaded | LoadingStates.Loading | LoadingStates.NotLoaded",
                    "references": {
                        "LoadingStates": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::LoadingStates"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool resource states injected from redux"
                },
                "attribute": "question-pool-resource-state",
                "reflect": false
            },
            "questionSetResourceState": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "LoadingStates",
                    "resolved": "LoadingStates.Error | LoadingStates.Loaded | LoadingStates.Loading | LoadingStates.NotLoaded",
                    "references": {
                        "LoadingStates": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::LoadingStates"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question set resource states injected from redux"
                },
                "attribute": "question-set-resource-state",
                "reflect": false
            },
            "shouldHideForFirstQuestionSet": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Hide back button on first question set"
                },
                "attribute": "should-hide-for-first-question-set",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "canSave": {},
            "areResourcesReady": {},
            "isReady": {},
            "isFirstRun": {},
            "isFirstQnSet": {},
            "isDisabled": {}
        };
    }
    static get watchers() {
        return [{
                "propName": "isReady",
                "methodName": "onIsReady"
            }, {
                "propName": "answersResourceState",
                "methodName": "onAnswersResourcesStateChange"
            }, {
                "propName": "applicationFormResourceState",
                "methodName": "onApplicationFormResourceStateChange"
            }, {
                "propName": "questionPoolResourceState",
                "methodName": "onQuestionPoolResourcesStateChange"
            }, {
                "propName": "questionSetResourceState",
                "methodName": "onQuestionSetResourcesStateChange"
            }, {
                "propName": "activeQuestionSet",
                "methodName": "onActiveQuestionSetChange"
            }, {
                "propName": "isFirstRun",
                "methodName": "onIsFirstRun"
            }, {
                "propName": "questionSets",
                "methodName": "onQuestionSetsChange"
            }, {
                "propName": "queue",
                "methodName": "onQueueChange"
            }];
    }
    static get listeners() {
        return [{
                "name": "navigateBack",
                "method": "handleNavigateBack",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
