import { checkAnswerQueue } from "@boldpenguin/sdk-redux";
import store from "../../services/redux-store.service";
export class BpSdkQueueContainer {
    constructor() {
        this.heartbeatInterval = undefined;
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, {
                checkAnswerQueue,
            });
            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 is() { return "bp-sdk-queue-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-queue-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-queue-container.css"]
        };
    }
    static get properties() {
        return {
            "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 injected from redux"
                }
            }
        };
    }
    static get watchers() {
        return [{
                "propName": "queue",
                "methodName": "onQueueChange"
            }];
    }
}
