import { h } from "@stencil/core";
export class BpSdkStepperGroupContainer {
    constructor() {
        this.handleStepperClick = (answerId) => {
            const index = this.questionGroup.answers.findIndex((answer) => answer.id === answerId);
            this.currentActiveStepper = index >= 0 ? index : 0;
        };
        this.handleButtonClick = (answerId, direction) => {
            if (direction === 'forward') {
                this.currentActiveStepper += 1;
            }
            else {
                this.currentActiveStepper -= 1;
            }
        };
        this.questionGroup = undefined;
        this.currentActiveStepper = 0;
        this.currentActiveStepperId = undefined;
        this.previousStep = null;
        this.nextStep = null;
        this.buttonDisable = false;
        this.useExternal = false;
    }
    onCurrentActiveStepChange(index) {
        var _a, _b, _c, _d, _e, _f;
        this.currentActiveStepperId = (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.answers[index]) === null || _b === void 0 ? void 0 : _b.id;
        this.previousStep = (_d = (_c = this.questionGroup) === null || _c === void 0 ? void 0 : _c.answers[index - 1]) !== null && _d !== void 0 ? _d : null;
        this.nextStep = (_f = (_e = this.questionGroup) === null || _e === void 0 ? void 0 : _e.answers[index + 1]) !== null && _f !== void 0 ? _f : null;
        this.updateButtonDisabled();
    }
    onQuestionGroupChange(questionGroup) {
        var _a, _b;
        if (!!questionGroup) {
            this.currentActiveStepperId = (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.answers[this.currentActiveStepper]) === null || _b === void 0 ? void 0 : _b.id;
            this.updateButtonDisabled();
        }
    }
    resetState() {
        var _a, _b;
        this.currentActiveStepper = 0;
        this.previousStep = null;
        this.nextStep = (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.answers[this.currentActiveStepper + 1]) !== null && _b !== void 0 ? _b : null;
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-stepper-group-container');
        customElements.whenDefined('bp-sdk-external-stepper-group-container').then(() => {
            this.useExternal = true;
        });
        this.onCurrentActiveStepChange(this.currentActiveStepper);
        this.onQuestionGroupChange(this.questionGroup);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderCurrentQuestionGroup(currentActiveStepperId) {
        var _a;
        const currentGroup = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.nestedGroupsWithAnswers.get(currentActiveStepperId);
        return (h("div", { class: "bp-sdk-stepper-group-container__current-group" }, h("bp-sdk-stepper-group", { questionGroup: currentGroup })));
    }
    renderExternal() {
        return h("bp-sdk-external-stepper-group-container", { questionGroup: this.questionGroup });
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-stepper-group-container__container" }, this.renderCurrentQuestionGroup(this.currentActiveStepperId), h("bp-sdk-stepper-group-button", { answer: this.previousStep, direction: "backward", buttonDisabled: false, handleStepperButtonClick: this.handleButtonClick }), h("bp-sdk-stepper-group-button", { answer: this.nextStep, direction: "forward", buttonDisabled: this.buttonDisable, handleStepperButtonClick: this.handleButtonClick })));
    }
    updateButtonDisabled() {
        var _a;
        if (!!this.currentActiveStepperId) {
            const group = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.nestedGroupsWithAnswers.get(this.currentActiveStepperId);
            const aggregateAnswers = [
                ...group.answers,
                ...group.answers.filter((answer) => {
                    if (group.nestedGroupsWithAnswers.has(answer.id)) {
                        return group.nestedGroupsWithAnswers.get(answer.id).answers;
                    }
                }),
            ];
            this.buttonDisable = !aggregateAnswers.every((answer) => { var _a; return !!answer.choice_id || ((_a = answer.choice_ids) === null || _a === void 0 ? void 0 : _a.length) > 0 || !!answer.value || !answer.required || answer.display_type === 'private'; });
        }
    }
    static get is() { return "bp-sdk-stepper-group-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-stepper-group-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-stepper-group-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"
                }
            }
        };
    }
    static get states() {
        return {
            "currentActiveStepper": {},
            "currentActiveStepperId": {},
            "previousStep": {},
            "nextStep": {},
            "buttonDisable": {},
            "useExternal": {}
        };
    }
    static get watchers() {
        return [{
                "propName": "currentActiveStepper",
                "methodName": "onCurrentActiveStepChange"
            }, {
                "propName": "questionGroup",
                "methodName": "onQuestionGroupChange"
            }];
    }
}
