'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 bpSdkCustomEventContainerCss = "";
const BpSdkCustomEventContainerStyle0 = bpSdkCustomEventContainerCss;

const BpSdkCustomEventContainer = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.activeQuestionSetChange = index.createEvent(this, "activeQuestionSetChange", 7);
        this.formLoaded = index.createEvent(this, "formLoaded", 7);
        this.userAuthenticationChanged = index.createEvent(this, "userAuthenticationChanged", 7);
        this.quotesChanged = index.createEvent(this, "quotesChanged", 7);
        this.quoteSuccess = index.createEvent(this, "quoteSuccess", 7);
        this.quoteFailure = index.createEvent(this, "quoteFailure", 7);
        this.answerStateChanged = index.createEvent(this, "answerStateChanged", 7);
        this.questionSetCompletionChanged = index.createEvent(this, "questionSetCompletionChanged", 7);
        this.questionSetAdded = index.createEvent(this, "questionSetAdded", 7);
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = reduxStore_service.store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSet: index_esm_min.Oe(state),
                applicationForm: index_esm_min._e(state),
                isFormLoaded: index_esm_min.jN(state),
                isUserAuthenticated: index_esm_min.sp(state),
                quotes: index_esm_min.pt(state),
                answerResources: index_esm_min.uR(state),
                questionSets: index_esm_min.me(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 === index_esm_min$1.S.completed) {
                    this.quoteSuccess.emit(quote);
                }
                else if (quote.request_status === index_esm_min$1.S.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 (index.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 index.h("div", { class: "bp-sdk-custom-event-container__container" });
    }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "activeQuestionSet": ["onActiveQuestionSetChange"],
        "isFormLoaded": ["onIsFormLoadedChange"],
        "isUserAuthenticated": ["onAuthenticatedChange"],
        "quotes": ["onQuotesChange"],
        "answerResources": ["onAnswerResourcesChange"],
        "questionSets": ["onQuestionSetsChange"]
    }; }
};
BpSdkCustomEventContainer.style = BpSdkCustomEventContainerStyle0;

const bpSdkInterviewToolbarCss = "";
const BpSdkInterviewToolbarStyle0 = bpSdkInterviewToolbarCss;

const BpSdkInterviewToolbar = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-interview-toolbar');
        customElements.whenDefined('bp-sdk-external-interview-toolbar').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return index.h("bp-sdk-external-interview-toolbar", null);
    }
    renderInternal() {
        return (index.h("div", { class: "bp-sdk-interview-toolbar__container" }, index.h("bp-sdk-take-over-container", null)));
    }
};
BpSdkInterviewToolbar.style = BpSdkInterviewToolbarStyle0;

const bpSdkQueueContainerCss = "";
const BpSdkQueueContainerStyle0 = bpSdkQueueContainerCss;

const BpSdkQueueContainer = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.heartbeatInterval = undefined;
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            reduxStore_service.store.mapDispatchToProps(this, {
                checkAnswerQueue: index_esm_min.w_,
            });
            this.storeUnsubscribe = reduxStore_service.store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                queue: state.queues.answers.queue,
            };
        };
        this.queue = undefined;
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        if (this.heartbeatInterval)
            this.stopHeartbeat();
    }
    resetState() { }
    componentWillLoad() {
        this.onQueueChange(this.queue);
    }
    onQueueChange(queue, oldQueue) {
        // While there is a queue, send a heartbeat to make sure redux is parsing it
        if ((queue === null || queue === void 0 ? void 0 : queue.length) && !(oldQueue === null || oldQueue === void 0 ? void 0 : oldQueue.length) && this.heartbeatInterval === undefined) {
            this.heartbeatInterval = setInterval(() => {
                this.checkAnswerQueue();
            }, 250);
        }
        // If the queue has emptied, stop the heartbeat
        if (!(queue === null || queue === void 0 ? void 0 : queue.length) && (oldQueue === null || oldQueue === void 0 ? void 0 : oldQueue.length) && this.heartbeatInterval) {
            this.stopHeartbeat();
        }
    }
    stopHeartbeat() {
        clearInterval(this.heartbeatInterval);
        this.heartbeatInterval = undefined;
    }
    render() {
        return;
    }
    static get watchers() { return {
        "queue": ["onQueueChange"]
    }; }
};
BpSdkQueueContainer.style = BpSdkQueueContainerStyle0;

const bpSdkSetupContainerCss = "";
const BpSdkSetupContainerStyle0 = bpSdkSetupContainerCss;

const BpSdkSetupContainer = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            reduxStore_service.store.mapDispatchToProps(this, { getFeatures: index_esm_min.xo });
            this.storeUnsubscribe = reduxStore_service.store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                config: index_esm_min.up(state),
                expiresAt: state.auth.expiresAt,
                token: state.auth.token,
                isSdkLoaded: state.status.local.config === index_esm_min$1.a.Configured,
                authenticated: index_esm_min.AR(state) && index_esm_min.sp(state),
                hasPermission: index_esm_min.VN(state, { permission: index_esm_min$1.T.FeatureCheck }),
            };
        };
        this.checkIfReady = () => (this.isReady = this.authenticated && this.hasPermission && this.isSdkLoaded);
        this.disconnectedCallback = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
        };
        this.expiresAt = 0;
        this.token = undefined;
        this.authenticated = false;
        this.config = undefined;
        this.hasPermission = false;
        this.useExternal = false;
        this.isReady = false;
        this.isSdkLoaded = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-setup-container');
        customElements.whenDefined('bp-sdk-external-setup-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
    }
    onExpiresAtChange(expiresAt) {
        const diff = expiresAt - Date.now();
        if (diff > 0) {
            setTimeout(this.checkIfReady, diff);
        }
        this.checkIfReady();
    }
    onCheckAuthenticationChange() {
        this.checkIfReady();
    }
    onIsReadyChange(isReady) {
        if (isReady && this.getFeatures) {
            this.getFeatures({ config: this.config });
        }
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (index.h("bp-sdk-external-setup-container", { config: this.config, isReady: this.isReady, isSdkLoaded: this.isSdkLoaded, token: this.token }, index.h("slot", null)));
    }
    renderInternal() {
        return (index.h("div", { class: "bp-sdk-setup-container__container" }, index.h("slot", null)));
    }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "expiresAt": ["onExpiresAtChange"],
        "authenticated": ["onCheckAuthenticationChange"],
        "hasPermission": ["onCheckAuthenticationChange"],
        "isSdkLoaded": ["onCheckAuthenticationChange"],
        "isReady": ["onIsReadyChange"]
    }; }
};
BpSdkSetupContainer.style = BpSdkSetupContainerStyle0;

exports.bp_sdk_custom_event_container = BpSdkCustomEventContainer;
exports.bp_sdk_interview_toolbar = BpSdkInterviewToolbar;
exports.bp_sdk_queue_container = BpSdkQueueContainer;
exports.bp_sdk_setup_container = BpSdkSetupContainer;
