import { getFormId, getPartnerEngineConfig, removeQuestionPool } from "@boldpenguin/sdk-redux";
import { getIsTrailerQuestionGroup, getVehicleAnswers } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkVehicleQuestionPoolContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { removeQuestionPool });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                applicationId: getFormId(state),
                config: getPartnerEngineConfig(state),
            };
        };
        this.handleDeleteItem = () => {
            this.isDeleting = true;
            this.removeQuestionPool({
                answer_pool_id: this.questionPoolId,
                application_id: this.applicationId,
                config: this.config,
                callback: () => {
                    this.deletePool.emit(undefined);
                },
            });
            this.deletePool.emit(this.questionPoolId);
        };
        this.questionGroup = undefined;
        this.applicationId = undefined;
        this.config = undefined;
        this.useExternal = false;
        this.questionPool = undefined;
        this.questionPoolId = undefined;
        this.isDeleting = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    onQuestionGroupChange(questionGroup) {
        this.questionPoolId = questionGroup === null || questionGroup === void 0 ? void 0 : questionGroup.poolId;
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-vehicle-question-pool-container');
        customElements.whenDefined('bp-sdk-external-vehicle-question-pool-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.onQuestionGroupChange(this.questionGroup);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-vehicle-question-pool-container", { applicationId: this.applicationId, config: this.config, questionGroup: this.questionGroup }));
    }
    renderInternal() {
        return (!this.isDeleting &&
            !!this.questionPoolId && (h("bp-sdk-vehicle-panel-container", { canDelete: true, isVehicleTrailer: getIsTrailerQuestionGroup(this.questionGroup), onDeleteItem: this.handleDeleteItem, questionGroup: this.questionGroup, answers: getVehicleAnswers(this.questionGroup), shouldShowTrailers: this.questionGroup.order >= 1 || getIsTrailerQuestionGroup(this.questionGroup) })));
    }
    static get is() { return "bp-sdk-vehicle-question-pool-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-vehicle-question-pool-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-vehicle-question-pool-container.css"]
        };
    }
    static get properties() {
        return {
            "questionGroup": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IMappedQuestionGroupAnswers",
                    "resolved": "IMappedQuestionGroupAnswers",
                    "references": {
                        "IMappedQuestionGroupAnswers": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IMappedQuestionGroupAnswers"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question Group"
                }
            },
            "applicationId": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "application-id",
                "reflect": false
            },
            "config": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IPartnerEngineConfig",
                    "resolved": "IPartnerEngineConfig",
                    "references": {
                        "IPartnerEngineConfig": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IPartnerEngineConfig"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "questionPool": {},
            "questionPoolId": {},
            "isDeleting": {}
        };
    }
    static get events() {
        return [{
                "method": "deletePool",
                "name": "deletePool",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "questionGroup",
                "methodName": "onQuestionGroupChange"
            }];
    }
}
