import { Secret } from "../secret.js";
import { poll } from "../util/poll.js";
export function formatConnectionUri(details) {
    return {
        connection_uri: new Secret(details.connection_uri),
        connection_parameters: {
            database: details.connection_parameters.database,
            host: details.connection_parameters.host,
            port: 5432,
            user: details.connection_parameters.role,
            password: new Secret(details.connection_parameters.password),
        },
    };
}
export function formatRole(role) {
    return {
        ...role,
        password: role.password ? new Secret(role.password) : undefined,
    };
}
export async function waitForOperations(api, operations) {
    for (const operation of operations) {
        if (isOperationComplete(operation)) {
            continue;
        }
        await poll({
            description: `operation "${operation.id}"`,
            fn: () => api.getProjectOperation({
                path: {
                    project_id: operation.project_id,
                    operation_id: operation.id,
                },
            }),
            predicate: ({ data }) => {
                if (["error", "failed"].includes(data.operation.status)) {
                    throw new Error(`Operation ${operation.id} (${operation.action}) failed: ${data.operation.error}`);
                }
                return isOperationComplete(data.operation);
            },
        });
    }
}
const isOperationComplete = (operation) => ["finished", "failed", "error", "cancelled", "skipped"].includes(operation.status);
//# sourceMappingURL=utils.js.map