import { isDebugMode } from "@boldpenguin/sdk-redux";
import { ReduxAsyncError } from "@boldpenguin/sdk-types";
import store from "../../services/redux-store.service";
const SDK_ERRORS = [new ReduxAsyncError().name];
export class BpSdkErrorEventContainer {
    constructor() {
        this.handleError = (ev) => {
            try {
                const { error } = ev;
                if (SDK_ERRORS.includes(error === null || error === void 0 ? void 0 : error.name)) {
                    // Emit DOM event
                    this.sdkError.emit({ error });
                    // Send error to console if debug flag is on
                    this.isDebug && console.error(error);
                    // Send to error handler function if it's provided
                    this.errorHandler && this.errorHandler(error);
                }
            }
            catch (e) {
                // Swallow errors here, if we can't capture the error successfully just do nothing
            }
        };
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                isDebug: isDebugMode(state),
            };
        };
        this.errorHandler = undefined;
        this.isDebug = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        window.addEventListener('error', this.handleError);
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    render() {
        return null;
    }
    static get is() { return "bp-sdk-error-event-container"; }
    static get properties() {
        return {
            "errorHandler": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "(error: Error) => void | undefined",
                    "resolved": "(error: Error) => void",
                    "references": {
                        "Error": {
                            "location": "global",
                            "id": "global::Error"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Function hook that will be called with SDK Errors that get thrown"
                }
            },
            "isDebug": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Debug flag injected from redux store"
                },
                "attribute": "is-debug",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get events() {
        return [{
                "method": "sdkError",
                "name": "sdkError",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emits DOM event with any SDK errors that are thrown"
                },
                "complexType": {
                    "original": "{\n    error: Error;\n  }",
                    "resolved": "{ error: Error; }",
                    "references": {
                        "Error": {
                            "location": "global",
                            "id": "global::Error"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
}
