import { getGroupId } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
const vehicleQuestionCodes = {
    year: 'mqs_vehicle_year',
    make: 'mqs_vehicle_make',
    model: 'mqs_vehicle_model',
    configuration: 'mqs_vehicle_submodel',
};
export class BpSdkVehicleInterface {
    constructor() {
        this.questionGroup = undefined;
        this.canSave = true;
        this.isVehicleTrailer = false;
        this.useExternal = false;
        this.answerIdMap = {
            year: '',
            make: '',
            model: '',
            configuration: '',
        };
    }
    onQuestionGroupChange() {
        this.answerIdMap = this.buildAnswerIdMap();
    }
    resetState() {
        this.answerIdMap = {
            year: '',
            make: '',
            model: '',
            configuration: '',
        };
    }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-vehicle-interface');
        customElements.whenDefined('bp-sdk-external-vehicle-interface').then(() => {
            this.useExternal = true;
        });
        this.answerIdMap = this.buildAnswerIdMap();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return h("bp-sdk-external-vehicle-interface", { isVehicleTrailer: this.isVehicleTrailer, questionGroup: this.questionGroup });
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-vehicle-interface__container" }, h("bp-sdk-vehicle-years-container", { isVehicleTrailer: this.isVehicleTrailer, questionGroupId: getGroupId(this.questionGroup), code: vehicleQuestionCodes.year, answerId: this.answerIdMap.year, answeredBySource: this.answeredBySource(this.answerIdMap.year) }), h("bp-sdk-vehicle-makes-container", { isVehicleTrailer: this.isVehicleTrailer, questionGroupId: getGroupId(this.questionGroup), code: vehicleQuestionCodes.make, answerId: this.answerIdMap.make, answeredBySource: this.answeredBySource(this.answerIdMap.make) }), h("bp-sdk-vehicle-models-container", { isVehicleTrailer: this.isVehicleTrailer, questionGroupId: getGroupId(this.questionGroup), code: vehicleQuestionCodes.model, answerId: this.answerIdMap.model, answeredBySource: this.answeredBySource(this.answerIdMap.model) }), h("bp-sdk-vehicle-configurations-container", { questionGroupId: getGroupId(this.questionGroup), code: vehicleQuestionCodes.configuration, answerId: this.answerIdMap.configuration, answeredBySource: this.answeredBySource(this.answerIdMap.configuration) })));
    }
    buildAnswerIdMap() {
        var _a, _b, _c, _d, _e, _f;
        const answers = (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.answers) !== null && _b !== void 0 ? _b : [];
        const yearAnswer = answers.find((answer) => answer.question.code === vehicleQuestionCodes.year);
        const makeAnswer = answers.find((answer) => answer.question.code === vehicleQuestionCodes.make);
        const modelAnswer = answers.find((answer) => answer.question.code === vehicleQuestionCodes.model);
        const configurationAnswer = answers.find((answer) => answer.question.code === vehicleQuestionCodes.configuration);
        return {
            year: (_c = yearAnswer === null || yearAnswer === void 0 ? void 0 : yearAnswer.id) !== null && _c !== void 0 ? _c : '',
            make: (_d = makeAnswer === null || makeAnswer === void 0 ? void 0 : makeAnswer.id) !== null && _d !== void 0 ? _d : '',
            model: (_e = modelAnswer === null || modelAnswer === void 0 ? void 0 : modelAnswer.id) !== null && _e !== void 0 ? _e : '',
            configuration: (_f = configurationAnswer === null || configurationAnswer === void 0 ? void 0 : configurationAnswer.id) !== null && _f !== void 0 ? _f : '',
        };
    }
    answeredBySource(answerId) {
        var _a, _b;
        const answers = (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.answers) !== null && _b !== void 0 ? _b : [];
        const answerForId = answers.find((answer) => answer.id === answerId);
        if (answerForId === null || answerForId === void 0 ? void 0 : answerForId.answered_by_consumer) {
            return 'Consumer';
        }
        return answerForId === null || answerForId === void 0 ? void 0 : answerForId.answered_by_source;
    }
    static get is() { return "bp-sdk-vehicle-interface"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-vehicle-interface.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-vehicle-interface.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"
                }
            },
            "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"
            },
            "isVehicleTrailer": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is vehicle a trailer"
                },
                "attribute": "is-vehicle-trailer",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "answerIdMap": {}
        };
    }
    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": "onQuestionGroupChange"
            }];
    }
}
