import { formatFieldValue } from "../plugins/tables/utils";
function summarizeRecord(record) {
    const values = Object.values(record.data);
    const preview = values
        .slice(0, 3)
        .map((v) => formatFieldValue(v).replace(/\s+/g, " ").trim())
        .filter((s) => s.length > 0)
        .join(", ");
    if (!preview)
        return undefined;
    return preview.length > 60 ? preview.slice(0, 57) + "..." : preview;
}
function fetchRecords(sdk, params) {
    return sdk.listTableRecords({
        table: params.table,
        keyMode: "names",
    });
}
function recordChoices(records) {
    return records.map((record) => ({
        name: summarizeRecord(record) || record.id,
        value: record.id,
    }));
}
export const tableRecordIdResolver = {
    type: "dynamic",
    depends: ["table"],
    fetch: fetchRecords,
    prompt: (records) => ({
        type: "list",
        name: "record",
        message: "Select a record:",
        choices: recordChoices(records),
    }),
};
export const tableRecordIdsResolver = {
    type: "dynamic",
    depends: ["table"],
    fetch: fetchRecords,
    prompt: (records) => ({
        type: "checkbox",
        name: "records",
        message: "Select records to delete:",
        choices: recordChoices(records),
        validate: (value) => Array.isArray(value) && value.length > 0
            ? true
            : "Select at least one record",
    }),
};
