import { r as registerInstance, h } from './index-77a9c9e7.js';
import { t, U } from './index.esm.min-9889e78b.js';
import { l as lw, g as ew, t as tw, z as zh, i as $1 } from './index.esm.min-cd800f97.js';

const bpSdkApplicationLoadCss = "";
const BpSdkApplicationLoadStyle0 = bpSdkApplicationLoadCss;

const BpSdkApplicationLoad = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-application-load');
        customElements.whenDefined('bp-sdk-external-application-load').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return h("bp-sdk-external-application-load", null);
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-application-load__container" }, h("bp-sdk-spinner", null)));
    }
};
BpSdkApplicationLoad.style = BpSdkApplicationLoadStyle0;

const bpSdkPendingProcessContainerCss = "";
const BpSdkPendingProcessContainerStyle0 = bpSdkPendingProcessContainerCss;

const BpSdkPendingProcessContainer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.pendingProcesses = [];
        this.useExternal = false;
    }
    resetState() { }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-pending-process-container');
        customElements.whenDefined('bp-sdk-external-pending-process-container').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return h("bp-sdk-external-pending-process-container", { pendingProcesses: this.pendingProcesses });
    }
    renderInternal() {
        return h("div", { class: "bp-sdk-pending-process-container__container" }, h("bp-sdk-pending-process", { pendingProcesses: this.pendingProcesses }));
    }
};
BpSdkPendingProcessContainer.style = BpSdkPendingProcessContainerStyle0;

const BpSdkQuestionSet = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.preventRefresh = (e) => {
            e.preventDefault();
            return false;
        };
        this.saveHandler = undefined;
        this.questionSet = undefined;
        this.areResourcesReady = undefined;
        this.locked = undefined;
        this.canSave = true;
        this.shouldRenderQuestionGroup = undefined;
        this.questionGroupsWithAnswersAsKey = new Map();
        this.useExternal = false;
        this.questions = undefined;
        this.questionGroups = {};
        this.parentIds = [];
        this.answers = [];
        this.deletedPool = undefined;
        this._answers = [];
    }
    onQuestionSetChange(questionSet) {
        var _a;
        this.questionGroups = lw(questionSet, this.questionGroups);
        this.parentIds = ew(this.questionGroups);
        const questionSetAnswers = (_a = questionSet === null || questionSet === void 0 ? void 0 : questionSet.answers) !== null && _a !== void 0 ? _a : [];
        this.answers = questionSetAnswers;
    }
    onAnswersChange(answers) {
        const answersWithPoolAnswersRemoved = tw(answers, this.deletedPool);
        this._answers = this.sortAnswers(answersWithPoolAnswersRemoved);
    }
    onDeletedPoolChange(poolId) {
        this._answers = tw(this.answers, poolId);
    }
    onSaveQuestionSet() {
        this.saveHandler(this.questionSet);
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-question-set');
        customElements.whenDefined('bp-sdk-external-question-set').then(() => {
            this.useExternal = true;
        });
        this.onQuestionSetChange(this.questionSet);
    }
    /**
     * Moves any coverage limit anchors to the last position in the answer list
     */
    sortAnswers(answers) {
        return answers.sort((answer1, answer2) => {
            const isCoverageLimitAnchor1 = this.isCoverageAnswer(answer1);
            const isCoverageLimitAnchor2 = this.isCoverageAnswer(answer2);
            let comparisonResult = 0;
            if (!isCoverageLimitAnchor1 && isCoverageLimitAnchor2) {
                comparisonResult = -1;
            }
            else if (isCoverageLimitAnchor1 && !isCoverageLimitAnchor2) {
                comparisonResult = 1;
            }
            return comparisonResult;
        });
    }
    isCoverageAnswer(answer) {
        return answer.question.field_type === t.Anchor && answer.question.tags.includes(U.CoverageLimit);
    }
    inQuestionGroup(answer) {
        return this.questionSet.question_set.question_groups.some((qg) => {
            return qg.question_ids.some((id) => id === answer.question.id);
        });
    }
    getAnswerPool(parentGroupId, poolId = '') {
        var _a, _b;
        const groupMap = (_a = this.questionGroups[poolId]) !== null && _a !== void 0 ? _a : {};
        return (_b = groupMap[parentGroupId]) !== null && _b !== void 0 ? _b : { answers: [] };
    }
    onDeletePool({ detail }) {
        this.deletedPool = detail;
    }
    render() {
        return (h("div", { key: '8f0819e08c65d548c8553508a3478b04afea8121', class: "bp-sdk-question-set" }, this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()));
    }
    renderExternal() {
        return (h("bp-sdk-external-question-set", { areResourcesReady: this.areResourcesReady, canSave: this.canSave, locked: this.locked, questionSet: this.questionSet, saveHandler: this.saveHandler, questionGroupsWithAnswersAsKey: this.questionGroupsWithAnswersAsKey, shouldRenderQuestionGroup: this.shouldRenderQuestionGroup }));
    }
    renderInternal() {
        return (h("form", { onSubmit: this.preventRefresh }, h("fieldset", { class: "bp-sdk-question-set-fieldset-wrapper", disabled: this.locked }, this._answers.map((answer) => this.renderAnswer(answer)))));
    }
    renderAnswer(answer) {
        var _a;
        if (!answer) {
            return null;
        }
        const shouldRenderGroup = this.shouldRenderQuestionGroup(this.questionGroupsWithAnswersAsKey, answer);
        return [
            !this.inQuestionGroup(answer) && (h("div", { class: zh('bp-sdk-question-set bp-sdk-question-set__input', {
                    'bp-sdk-coverage-width': ((_a = this.questionSet) === null || _a === void 0 ? void 0 : _a.order) === 1,
                }), "data-answerId": answer.id }, h("bp-sdk-dynamic-input-redux-container", { answer: answer }))),
            shouldRenderGroup && this.renderDynamicGroup(answer),
        ];
    }
    renderDynamicGroup(answer) {
        const mappedGroup = $1(answer, this.questionGroupsWithAnswersAsKey);
        // This mapped group is rendered in a loop, so each item needs a unique key; otherwise Stencil may
        // try to reuse components and that can lead to corrupted state and odd behavior
        return mappedGroup && h("bp-sdk-question-group-container", { key: answer.id, questionGroup: mappedGroup });
    }
    static get watchers() { return {
        "questionSet": ["onQuestionSetChange"],
        "answers": ["onAnswersChange"],
        "deletedPool": ["onDeletedPoolChange"]
    }; }
};

const BpSdkQuestionSetEmpty = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.questionSet = undefined;
        this.useExternal = false;
    }
    resetState() { }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-question-set-empty');
        customElements.whenDefined('bp-sdk-external-question-set-empty').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return h("bp-sdk-external-question-set-empty", { questionSet: this.questionSet });
    }
    renderInternal() {
        return h("div", { class: "bp-sdk-question-set-empty__container" }, h("p", { class: "bp-sdk-question-set-empty__message" }, "Thanks! There are no additional questions to answer, please continue."));
    }
};

const bpSdkQuestionSetHeaderCss = "";
const BpSdkQuestionSetHeaderStyle0 = bpSdkQuestionSetHeaderCss;

const BpSdkQuestionSetHeader = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.applicationId = undefined;
        this.code = undefined;
        this.isActive = undefined;
        this.isCompleted = undefined;
        this.name = undefined;
        this.setId = undefined;
        this.setTitle = undefined;
        this.tenantId = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-question-set-header');
        customElements.whenDefined('bp-sdk-external-question-set-header').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-question-set-header", { applicationId: this.applicationId, code: this.code, isActive: this.isActive, isCompleted: this.isCompleted, name: this.name, setId: this.setId, setTitle: this.setTitle, tenantId: this.tenantId }));
    }
    renderInternal() {
        return h("div", { class: "bp-sdk-question-set-header__container" });
    }
};
BpSdkQuestionSetHeader.style = BpSdkQuestionSetHeaderStyle0;

export { BpSdkApplicationLoad as bp_sdk_application_load, BpSdkPendingProcessContainer as bp_sdk_pending_process_container, BpSdkQuestionSet as bp_sdk_question_set, BpSdkQuestionSetEmpty as bp_sdk_question_set_empty, BpSdkQuestionSetHeader as bp_sdk_question_set_header };
