import { loopGet, loopPost, loopPut, withTeamKey } from "../lib/loop-api.js";
export async function supplier(params) {
    const { accountId, teamName, action, code } = params;
    // jobId is required for all actions except maintenance-submit-quote
    const jobId = params.jobId;
    if (action !== "maintenance-submit-quote" && jobId == null) {
        throw new Error(`jobId is required for action "${action}".`);
    }
    if (action === "maintenance-jobs") {
        const result = await withTeamKey(accountId, teamName, "supplier", "loop-supplier", async (apiKey) => {
            return loopGet(apiKey, `/supplier/maintenance/${encodeURIComponent(code)}/${jobId}/job-list`, "loop-supplier", teamName);
        });
        return JSON.stringify(result, null, 2);
    }
    if (action === "maintenance-complete") {
        await withTeamKey(accountId, teamName, "supplier", "loop-supplier", async (apiKey) => {
            return loopPost(apiKey, `/supplier/maintenance/${encodeURIComponent(code)}/${jobId}/complete`, {}, "loop-supplier", teamName);
        });
        return `Maintenance job ${jobId} marked complete via team "${teamName}".`;
    }
    if (action === "maintenance-quotes") {
        const result = await withTeamKey(accountId, teamName, "supplier", "loop-supplier", async (apiKey) => {
            return loopGet(apiKey, `/supplier/maintenance/${encodeURIComponent(code)}/${jobId}/quote-list`, "loop-supplier", teamName);
        });
        return JSON.stringify(result, null, 2);
    }
    if (action === "maintenance-submit-quote") {
        if (!params.quoteId)
            throw new Error("quoteId is required for maintenance-submit-quote.");
        await withTeamKey(accountId, teamName, "supplier", "loop-supplier", async (apiKey) => {
            return loopPut(apiKey, `/supplier/maintenance/${encodeURIComponent(code)}/${params.quoteId}/quote-for-job`, params.quoteData ?? {}, "loop-supplier", teamName);
        });
        return `Quote ${params.quoteId} submitted via team "${teamName}".`;
    }
    if (action === "board-jobs") {
        const result = await withTeamKey(accountId, teamName, "supplier", "loop-supplier", async (apiKey) => {
            return loopGet(apiKey, `/supplier/board-contractor/${encodeURIComponent(code)}/${jobId}/job-list`, "loop-supplier", teamName);
        });
        return JSON.stringify(result, null, 2);
    }
    if (action === "board-complete") {
        await withTeamKey(accountId, teamName, "supplier", "loop-supplier", async (apiKey) => {
            return loopPost(apiKey, `/supplier/board-contractor/${encodeURIComponent(code)}/${jobId}/complete`, params.completionData ?? {}, "loop-supplier", teamName);
        });
        return `Board contractor job ${jobId} marked complete via team "${teamName}".`;
    }
    throw new Error(`Unknown supplier action: ${action}`);
}
//# sourceMappingURL=supplier.js.map