import { getMessagesForApplicationForm } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkMessageContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                messages: getMessagesForApplicationForm(state),
            };
        };
        this.messages = undefined;
        this.useExternal = false;
        this.lastMessage = undefined;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-message-container');
        customElements.whenDefined('bp-sdk-external-message-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
    }
    handleMessagesUpdate(messages) {
        this.lastMessage = messages[this.messages.length - 1];
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    componentWillUpdate() {
        this.handleSubscriptions();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return h("bp-sdk-external-message-container", { messages: this.messages });
    }
    renderInternal() {
        return this.lastMessage && h("bp-sdk-message", { message: this.lastMessage });
    }
    static get is() { return "bp-sdk-message-container"; }
    static get properties() {
        return {
            "messages": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IMessage[]",
                    "resolved": "IMessage[]",
                    "references": {
                        "IMessage": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IMessage"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Application form messages injected from redux\n\nLegacy messages target the application form and are displayed here. Non-legacy messages can be attached to\ncomponents other than the application form and will be handled elsewhere."
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "lastMessage": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "messages",
                "methodName": "handleMessagesUpdate"
            }];
    }
}
