import { concatPaginated, toIterable } from "../utils/pagination-utils";
export const tableIdResolver = {
    type: "dynamic",
    requireCapabilities: ["canIncludeSharedTables"],
    fetch: async (sdk) => {
        const context = sdk.context;
        const includeShared = await context.hasCapability?.("canIncludeSharedTables");
        // Unlike connections, the tables API does not return personal tables
        // first, so we use concatPaginated to hoist own tables above shared.
        if (includeShared) {
            return toIterable(concatPaginated({
                sources: [
                    () => sdk.listTables(),
                    () => sdk.listTables({ includeShared: true }),
                ],
                dedupe: (table) => table.id,
            }));
        }
        return toIterable(sdk.listTables());
    },
    prompt: (tables) => ({
        type: "list",
        name: "table",
        message: "Select a table:",
        choices: tables.map((table) => ({
            name: table.name,
            value: table.id,
        })),
    }),
};
