import { VehicleFlowTypes } from "@boldpenguin/sdk-types";
import { getCommercialAutoFlowQuestion, getGroupId } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
export class BpSdkVehicleGroup {
    constructor() {
        this.saveButtonClick = () => {
            switch (this.selectedCAFlow) {
                case VehicleFlowTypes.ymm:
                    this.saveVehicle.emit(VehicleFlowTypes.ymm);
                    break;
                case VehicleFlowTypes.vin:
                    this.saveVehicle.emit(VehicleFlowTypes.vin);
                    break;
                case 'trailer':
                    this.saveVehicle.emit(VehicleFlowTypes.trailers);
                    break;
            }
        };
        this.canSave = true;
        this.questionGroup = undefined;
        this.isVehicleTrailer = false;
        this.shouldShowTrailers = true;
        this.handleDelete = undefined;
        this.useExternal = false;
        this.flowAnswer = undefined;
        this.selectedCAFlow = 'none';
    }
    onQuestionSetChange(questionGroup) {
        this.flowAnswer = getCommercialAutoFlowQuestion(questionGroup);
        this.selectedCAFlow = this.getSelectedVehicleInput();
    }
    resetState() {
        this.flowAnswer = getCommercialAutoFlowQuestion(this.questionGroup);
    }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-vehicle-group');
        customElements.whenDefined('bp-sdk-external-vehicle-group').then(() => {
            this.useExternal = true;
        });
        this.onQuestionSetChange(this.questionGroup);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderVehicleQuestions() {
        switch (this.selectedCAFlow) {
            case 'ymm':
                return h("bp-sdk-vehicle-interface", { questionGroup: this.questionGroup, canSave: this.canSave });
            case 'vin':
                return h("bp-sdk-vehicle-vin-container", { questionGroupId: getGroupId(this.questionGroup) });
            case 'trailer':
                return this.shouldShowTrailers ? (h("bp-sdk-vehicle-interface", { questionGroup: this.questionGroup, isVehicleTrailer: true, canSave: this.canSave })) : ('');
            default:
                return '';
        }
    }
    renderExternal() {
        return (h("bp-sdk-external-vehicle-group", { canSave: this.canSave, isVehicleTrailer: this.isVehicleTrailer, questionGroup: this.questionGroup, shouldShowTrailers: this.shouldShowTrailers, handleDelete: this.handleDelete }));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-vehicle-group__container" }, h("h2", null, "Vehicle Information"), h("bp-sdk-dynamic-input-redux-container", { answer: this.flowAnswer, label: "Choose how to add your vehicle" }), this.renderVehicleQuestions(), h("div", { class: "bp-sdk-row" }, h("bp-sdk-vehicle-delete-button", { onClick: this.handleDelete }), h("bp-sdk-vehicle-save-button", { buttonDisabled: !this.canSave, onClick: this.saveButtonClick, text: "Save Vehicle" }))));
    }
    getSelectedVehicleInput() {
        var _a;
        if (!this.flowAnswer) {
            return 'none';
        }
        const selectedCAFlow = this.flowAnswer.choice_id;
        const aliasOfSelectedChoice = (_a = this.flowAnswer.question.choices.find((choice) => choice.id === selectedCAFlow)) === null || _a === void 0 ? void 0 : _a.value;
        switch (aliasOfSelectedChoice) {
            case 'Year / Make / Model':
                return 'ymm';
            case 'VIN':
                return 'vin';
            case 'Trailer':
                return 'trailer';
            default:
                return 'none';
        }
    }
    static get is() { return "bp-sdk-vehicle-group"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-vehicle-group.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-vehicle-group.css"]
        };
    }
    static get properties() {
        return {
            "canSave": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Can Save"
                },
                "attribute": "can-save",
                "reflect": false,
                "defaultValue": "true"
            },
            "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"
                }
            },
            "isVehicleTrailer": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is vehicle trailer"
                },
                "attribute": "is-vehicle-trailer",
                "reflect": false,
                "defaultValue": "false"
            },
            "shouldShowTrailers": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Should show trailers"
                },
                "attribute": "should-show-trailers",
                "reflect": false,
                "defaultValue": "true"
            },
            "handleDelete": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "() => void",
                    "resolved": "() => void",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "flowAnswer": {},
            "selectedCAFlow": {}
        };
    }
    static get events() {
        return [{
                "method": "saveVehicle",
                "name": "saveVehicle",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "VehicleFlowTypes",
                    "resolved": "VehicleFlowTypes.trailers | VehicleFlowTypes.vin | VehicleFlowTypes.ymm",
                    "references": {
                        "VehicleFlowTypes": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::VehicleFlowTypes"
                        }
                    }
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "questionGroup",
                "methodName": "onQuestionSetChange"
            }];
    }
}
