import { getAnswersResourceState, getApplicationForm, getCurrentActiveQuestionSet, getQuestionSets, getQuotes, isFormLoaded, isUserAuthenticated, } from "@boldpenguin/sdk-redux";
import { QuoteRequestRequestTypes, } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkCustomEventContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSet: getCurrentActiveQuestionSet(state),
                applicationForm: getApplicationForm(state),
                isFormLoaded: isFormLoaded(state),
                isUserAuthenticated: isUserAuthenticated(state),
                quotes: getQuotes(state),
                answerResources: getAnswersResourceState(state),
                questionSets: getQuestionSets(state),
            };
        };
        this.activeQuestionSet = undefined;
        this.applicationForm = undefined;
        this.isFormLoaded = undefined;
        this.isUserAuthenticated = undefined;
        this.quotes = undefined;
        this.answerResources = {};
        this.questionSets = [];
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    componentWillLoad() {
        customElements.whenDefined(`bp-sdk-external-custom-event-container`).then(() => {
            this.useExternal = true;
        });
    }
    onActiveQuestionSetChange(activeQuestionSet, oldActiveQuestionSet) {
        if (!activeQuestionSet) {
            return;
        }
        if ((!oldActiveQuestionSet && !!activeQuestionSet) || activeQuestionSet.question_set_id !== (oldActiveQuestionSet === null || oldActiveQuestionSet === void 0 ? void 0 : oldActiveQuestionSet.question_set_id)) {
            this.activeQuestionSetChange.emit(activeQuestionSet);
        }
    }
    onIsFormLoadedChange(isFormLoaded) {
        if (isFormLoaded) {
            this.formLoaded.emit(this.applicationForm);
        }
    }
    onAuthenticatedChange(isAuthenticated) {
        this.userAuthenticationChanged.emit(isAuthenticated);
    }
    onQuotesChange(newQuotes, oldQuotes) {
        this.quotesChanged.emit(newQuotes);
        // Run through and emit all quotes that have changed since the previous state as success/fail events
        newQuotes.map((quote) => {
            const existing = oldQuotes === null || oldQuotes === void 0 ? void 0 : oldQuotes.find((oldQuote) => quote.id === oldQuote.id);
            let changed = true;
            if (existing) {
                changed = existing.request_status !== quote.request_status;
            }
            if (changed) {
                if (quote.request_status === QuoteRequestRequestTypes.completed) {
                    this.quoteSuccess.emit(quote);
                }
                else if (quote.request_status === QuoteRequestRequestTypes.failed) {
                    this.quoteFailure.emit(quote);
                }
            }
        });
    }
    onAnswerResourcesChange(newResources, oldResources) {
        Object.keys(newResources).map((answerId) => {
            const existingValue = oldResources && oldResources[answerId];
            const currentValue = newResources[answerId];
            const changed = existingValue !== currentValue;
            if (changed) {
                this.answerStateChanged.emit({ answerId, loadingState: currentValue });
            }
        });
    }
    onQuestionSetsChange(newQuestionSets, oldQuestionSets = []) {
        if (!newQuestionSets) {
            return;
        }
        newQuestionSets.map((newQuestionSet) => {
            const existing = oldQuestionSets === null || oldQuestionSets === void 0 ? void 0 : oldQuestionSets.find((oldQuestionSet) => oldQuestionSet.question_set_id === newQuestionSet.question_set_id);
            let completionChanged = true;
            if (existing) {
                completionChanged = existing.is_completed !== newQuestionSet.is_completed;
            }
            else {
                // If question set didn't exist before, it's newly added
                this.questionSetAdded.emit(newQuestionSet);
            }
            if (completionChanged) {
                this.questionSetCompletionChanged.emit(newQuestionSet);
            }
        });
    }
    componentWillUpdate() {
        this.handleSubscriptions();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-custom-event-container", { activeQuestionSet: this.activeQuestionSet, applicationForm: this.applicationForm, isFormLoaded: this.isFormLoaded, isUserAuthenticated: this.isUserAuthenticated, quotes: this.quotes, answerResources: this.answerResources, questionSets: this.questionSets }));
    }
    renderInternal() {
        return h("div", { class: "bp-sdk-custom-event-container__container" });
    }
    static get is() { return "bp-sdk-custom-event-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-custom-event-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-custom-event-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": ""
                }
            },
            "applicationForm": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IApplicationForm",
                    "resolved": "IApplicationForm",
                    "references": {
                        "IApplicationForm": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IApplicationForm"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            },
            "isFormLoaded": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "is-form-loaded",
                "reflect": false
            },
            "isUserAuthenticated": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "is-user-authenticated",
                "reflect": false
            },
            "quotes": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IQuotesState[]",
                    "resolved": "IQuotesState[]",
                    "references": {
                        "IQuotesState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuotesState"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            },
            "answerResources": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "{ [answerId: string]: LoadingStates }",
                    "resolved": "{ [answerId: string]: LoadingStates; }",
                    "references": {
                        "LoadingStates": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::LoadingStates"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "defaultValue": "{}"
            },
            "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": ""
                },
                "defaultValue": "[]"
            }
        };
    }
    static get states() {
        return {
            "useExternal": {}
        };
    }
    static get events() {
        return [{
                "method": "activeQuestionSetChange",
                "name": "activeQuestionSetChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits the entire question set when the active question set changes"
                },
                "complexType": {
                    "original": "IApplicationFormQuestionSet",
                    "resolved": "IApplicationFormQuestionSet",
                    "references": {
                        "IApplicationFormQuestionSet": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IApplicationFormQuestionSet"
                        }
                    }
                }
            }, {
                "method": "formLoaded",
                "name": "formLoaded",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when an application form is loaded"
                },
                "complexType": {
                    "original": "IApplicationForm",
                    "resolved": "IApplicationForm",
                    "references": {
                        "IApplicationForm": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IApplicationForm"
                        }
                    }
                }
            }, {
                "method": "userAuthenticationChanged",
                "name": "userAuthenticationChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits a boolean indicating whether the user is authenticated or not"
                },
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                }
            }, {
                "method": "quotesChanged",
                "name": "quotesChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits all quotes when any quotes change"
                },
                "complexType": {
                    "original": "IQuotesState[]",
                    "resolved": "IQuotesState[]",
                    "references": {
                        "IQuotesState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuotesState"
                        }
                    }
                }
            }, {
                "method": "quoteSuccess",
                "name": "quoteSuccess",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits a quote when it is successful"
                },
                "complexType": {
                    "original": "IQuotesState",
                    "resolved": "IQuotesState",
                    "references": {
                        "IQuotesState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuotesState"
                        }
                    }
                }
            }, {
                "method": "quoteFailure",
                "name": "quoteFailure",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits a quote when it has failed"
                },
                "complexType": {
                    "original": "IQuotesState",
                    "resolved": "IQuotesState",
                    "references": {
                        "IQuotesState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuotesState"
                        }
                    }
                }
            }, {
                "method": "answerStateChanged",
                "name": "answerStateChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits an answerId when it's loading state changes"
                },
                "complexType": {
                    "original": "{ answerId: string; loadingState: LoadingStates }",
                    "resolved": "{ answerId: string; loadingState: LoadingStates; }",
                    "references": {
                        "LoadingStates": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::LoadingStates"
                        }
                    }
                }
            }, {
                "method": "questionSetCompletionChanged",
                "name": "questionSetCompletionChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits a question set when it's completion state has changed"
                },
                "complexType": {
                    "original": "IApplicationFormQuestionSet",
                    "resolved": "IApplicationFormQuestionSet",
                    "references": {
                        "IApplicationFormQuestionSet": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IApplicationFormQuestionSet"
                        }
                    }
                }
            }, {
                "method": "questionSetAdded",
                "name": "questionSetAdded",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits a question set when it has been added to the application form"
                },
                "complexType": {
                    "original": "IApplicationFormQuestionSet",
                    "resolved": "IApplicationFormQuestionSet",
                    "references": {
                        "IApplicationFormQuestionSet": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IApplicationFormQuestionSet"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "activeQuestionSet",
                "methodName": "onActiveQuestionSetChange"
            }, {
                "propName": "isFormLoaded",
                "methodName": "onIsFormLoadedChange"
            }, {
                "propName": "isUserAuthenticated",
                "methodName": "onAuthenticatedChange"
            }, {
                "propName": "quotes",
                "methodName": "onQuotesChange"
            }, {
                "propName": "answerResources",
                "methodName": "onAnswerResourcesChange"
            }, {
                "propName": "questionSets",
                "methodName": "onQuestionSetsChange"
            }];
    }
}
