import { Resource } from "../resource.js";
import { createVercelApi } from "./api.js";
/**
 * Add and manage domains for Vercel projects.
 *
 * @example
 * // Minimal
 * const domain = await ProjectDomain("my-app.com", {
 *   name: "my-app.com",
 *   project: "prj_123",
 * });
 *
 * @example
 * // With accessToken
 * const domain = await ProjectDomain("my-app.com", {
 *   accessToken: alchemy.secret(process.env.VERCEL_ACCESS_TOKEN),
 *   name: "my-app.com",
 *   project: "prj_123",
 * });
 *
 * @example
 * // With redirect
 * const domain = await ProjectDomain("my-app.com", {
 *   name: "my-app.com",
 *   project: "prj_123",
 *   gitBranch: "main",
 *   redirect: "https://example.com",
 *   redirectStatusCode: 301,
 * });
 */
export const ProjectDomain = Resource("vercel::ProjectDomain", async function (_id, { accessToken, ...props }) {
    switch (this.phase) {
        case "create": {
            const api = await createVercelApi({
                baseUrl: "https://api.vercel.com/v10",
                accessToken,
            });
            const domain = (await api
                .post(`/projects/${props.project}/domains`, props)
                .then((res) => res.json()));
            return this.create({ ...props, ...domain });
        }
        case "update": {
            const { gitBranch, redirect, redirectStatusCode } = props;
            const api = await createVercelApi({
                baseUrl: "https://api.vercel.com/v9",
                accessToken,
            });
            const domain = (await api
                .patch(`/projects/${props.project}/domains/${this.output.name}`, {
                gitBranch,
                redirect,
                redirectStatusCode,
            })
                .then((res) => res.json()));
            return this.create({ ...props, ...domain });
        }
        case "delete": {
            const api = await createVercelApi({
                baseUrl: "https://api.vercel.com/v9",
                accessToken,
            });
            await api.delete(`/projects/${props.project}/domains/${this.output.name}`);
            return this.destroy();
        }
    }
});
//# sourceMappingURL=project-domain.js.map