import { r as registerInstance, c as createEvent, h, g as getElement } from './index-77a9c9e7.js';
import { O as Oe, E as _e, F as jN, J as sp, N as pt, Q as uR, b as me, R as w_, Y as xo, u as up, $ as AR, V as VN } from './index.esm.min-db2d29e8.js';
import { S, c as a, T } from './index.esm.min-9889e78b.js';
import { s as store } from './redux-store.service-dc54f48e.js';

const bpSdkCustomEventContainerCss = "";
const BpSdkCustomEventContainerStyle0 = bpSdkCustomEventContainerCss;

const BpSdkCustomEventContainer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.activeQuestionSetChange = createEvent(this, "activeQuestionSetChange", 7);
        this.formLoaded = createEvent(this, "formLoaded", 7);
        this.userAuthenticationChanged = createEvent(this, "userAuthenticationChanged", 7);
        this.quotesChanged = createEvent(this, "quotesChanged", 7);
        this.quoteSuccess = createEvent(this, "quoteSuccess", 7);
        this.quoteFailure = createEvent(this, "quoteFailure", 7);
        this.answerStateChanged = createEvent(this, "answerStateChanged", 7);
        this.questionSetCompletionChanged = createEvent(this, "questionSetCompletionChanged", 7);
        this.questionSetAdded = createEvent(this, "questionSetAdded", 7);
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                activeQuestionSet: Oe(state),
                applicationForm: _e(state),
                isFormLoaded: jN(state),
                isUserAuthenticated: sp(state),
                quotes: pt(state),
                answerResources: uR(state),
                questionSets: 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 === S.completed) {
                    this.quoteSuccess.emit(quote);
                }
                else if (quote.request_status === 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 (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" });
    }
    get el() { return 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) {
        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 h("bp-sdk-external-interview-toolbar", null);
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-interview-toolbar__container" }, h("bp-sdk-take-over-container", null)));
    }
};
BpSdkInterviewToolbar.style = BpSdkInterviewToolbarStyle0;

const bpSdkQueueContainerCss = "";
const BpSdkQueueContainerStyle0 = bpSdkQueueContainerCss;

const BpSdkQueueContainer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.heartbeatInterval = undefined;
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, {
                checkAnswerQueue: w_,
            });
            this.storeUnsubscribe = 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) {
        registerInstance(this, hostRef);
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { getFeatures: xo });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                config: up(state),
                expiresAt: state.auth.expiresAt,
                token: state.auth.token,
                isSdkLoaded: state.status.local.config === a.Configured,
                authenticated: AR(state) && sp(state),
                hasPermission: VN(state, { permission: 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 (h("bp-sdk-external-setup-container", { config: this.config, isReady: this.isReady, isSdkLoaded: this.isSdkLoaded, token: this.token }, h("slot", null)));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-setup-container__container" }, h("slot", null)));
    }
    get el() { return getElement(this); }
    static get watchers() { return {
        "expiresAt": ["onExpiresAtChange"],
        "authenticated": ["onCheckAuthenticationChange"],
        "hasPermission": ["onCheckAuthenticationChange"],
        "isSdkLoaded": ["onCheckAuthenticationChange"],
        "isReady": ["onIsReadyChange"]
    }; }
};
BpSdkSetupContainer.style = BpSdkSetupContainerStyle0;

export { BpSdkCustomEventContainer as bp_sdk_custom_event_container, BpSdkInterviewToolbar as bp_sdk_interview_toolbar, BpSdkQueueContainer as bp_sdk_queue_container, BpSdkSetupContainer as bp_sdk_setup_container };
