import { Container, Spacer, Text } from "@earendil-works/pi-tui";
import { formatAnswerScalar } from "../tool/format-answer.js";
import { HINT_PART_CANCEL, HINT_PART_ENTER, HINT_PART_NAV, HINT_PART_NOTES, HINT_PART_TAB, HINT_PART_TOGGLE, INCOMPLETE_WARNING_PREFIX, READY_PROMPT, REVIEW_HEADING, } from "./dialog-builder.js";
const NOTES_HEADER = "Notes:";
export class QuestionTabStrategy {
    constructor(config) {
        /** Spacer(1) + chatRow(1) + Spacer(1) + Text(hint, 1) = 4 rendered rows. */
        this.footerRowCount = 4;
        this.config = config;
    }
    headingRows(state) {
        const out = [];
        const question = this.config.questions[state.currentTab];
        // In multi-question mode the tab bar already shows the header; suppress the inline badge.
        if (!this.config.isMulti && question?.header && question.header.length > 0) {
            out.push(new Text(this.config.theme.bg("selectedBg", ` ${question.header} `), 1, 0));
            out.push(new Spacer(1));
        }
        if (question) {
            out.push(new Text(this.config.theme.bold(question.question), 1, 0));
            out.push(new Spacer(1));
        }
        return out;
    }
    bodyComponent(state) {
        const question = this.config.questions[state.currentTab];
        const mso = this.config.tabsByIndex[state.currentTab]?.multiSelect;
        if (question?.multiSelect === true && mso)
            return mso;
        return this.config.getPreviewPane();
    }
    bodyHeight(width, _state) {
        return this.config.getCurrentBodyHeight(width);
    }
    midRows(state) {
        if (!state.notesVisible)
            return [];
        return [
            new Text(this.config.theme.fg("muted", NOTES_HEADER), 1, 0),
            this.config.notesInput,
            new Spacer(1),
        ];
    }
    footerRows(state) {
        const question = this.config.questions[state.currentTab];
        return [
            new Spacer(1),
            this.config.chatRow,
            new Spacer(1),
            new Text(this.config.theme.fg("dim", buildHintText(question, this.config.isMulti, state)), 1, 0),
        ];
    }
}
export class SubmitTabStrategy {
    constructor(config) {
        /** Spacer(1) + Text(prompt, 1) + Spacer(1) + submitPicker(2) = 5 rendered rows. Fallback path lands at 5 via 2 trailing Spacer(1)s. */
        this.footerRowCount = 5;
        this.config = config;
    }
    headingRows(_state) {
        return [
            new Text(this.config.theme.bold(this.config.theme.fg("accent", REVIEW_HEADING)), 1, 0),
            new Spacer(1),
        ];
    }
    bodyComponent(state) {
        const c = new Container();
        for (let i = 0; i < this.config.questions.length; i++) {
            const q = this.config.questions[i];
            const a = state.answers.get(i);
            if (!a)
                continue;
            const label = q.header && q.header.length > 0 ? q.header : `Q${i + 1}`;
            const answerText = formatAnswerScalar(a, "summary");
            c.addChild(new Text(this.config.theme.fg("muted", ` ● ${label}`), 1, 0));
            c.addChild(new Text(`   ${this.config.theme.fg("muted", "→")} ${this.config.theme.fg("text", answerText)}`, 1, 0));
            if (a.notes && a.notes.length > 0) {
                c.addChild(new Text(this.config.theme.fg("dim", `     notes: ${a.notes}`), 1, 0));
            }
        }
        return c;
    }
    bodyHeight(width, state) {
        return this.bodyComponent(state).render(width).length;
    }
    midRows(_state) {
        return [];
    }
    footerRows(state) {
        const missing = [];
        for (let i = 0; i < this.config.questions.length; i++) {
            const q = this.config.questions[i];
            if (!state.answers.has(i)) {
                missing.push(q.header && q.header.length > 0 ? q.header : `Q${i + 1}`);
            }
        }
        const promptText = missing.length === 0
            ? this.config.theme.fg("muted", READY_PROMPT)
            : this.config.theme.fg("warning", `${INCOMPLETE_WARNING_PREFIX} ${missing.join(", ")}`);
        const out = [new Spacer(1), new Text(promptText, 1, 0), new Spacer(1)];
        if (this.config.submitPicker) {
            out.push(this.config.submitPicker);
        }
        else {
            // Padding when the picker isn't wired — keeps rendered row count at footerRowCount=5.
            out.push(new Spacer(1));
            out.push(new Spacer(1));
        }
        return out;
    }
}
/**
 * Build the controls hint line. Order is fixed so `HINT_SINGLE` / `HINT_MULTI`
 * remain contiguous substrings of the result:
 *   Enter · ↑/↓ [· Space toggle] [· n notes] [· Tab switch] · Esc
 */
export function buildHintText(question, isMulti, state) {
    const parts = [HINT_PART_ENTER, HINT_PART_NAV];
    if (question?.multiSelect === true)
        parts.push(HINT_PART_TOGGLE);
    if (question && question.multiSelect !== true && state.focusedOptionHasPreview && !state.notesVisible) {
        parts.push(HINT_PART_NOTES);
    }
    if (isMulti)
        parts.push(HINT_PART_TAB);
    parts.push(HINT_PART_CANCEL);
    return parts.join(" · ");
}
//# sourceMappingURL=tab-content-strategy.js.map