import { WorkspaceBackendError } from "./errors";
export class BaseGitProviderClient {
    joinPath(...parts) {
        const cleaned = parts
            .filter((p) => !!p)
            .map((p) => p.replace(/^\/+|\/+$/g, ""))
            .filter((p) => p.length > 0);
        return cleaned.join("/");
    }
    encodePath(path) {
        return path
            .split("/")
            .filter((p) => p.length > 0)
            .map((p) => encodeURIComponent(p))
            .join("/");
    }
    base64EncodeUtf8(value) {
        if (typeof globalThis.btoa === "function") {
            const bytes = new TextEncoder().encode(value);
            let binary = "";
            for (const b of bytes)
                binary += String.fromCharCode(b);
            return globalThis.btoa(binary);
        }
        const buf = globalThis.Buffer;
        if (buf && typeof buf.from === "function")
            return buf.from(value, "utf8").toString("base64");
        throw new Error("Base64 encoding not available in this environment");
    }
    base64DecodeUtf8(value) {
        const cleaned = value.replace(/\s+/g, "");
        if (typeof globalThis.atob === "function") {
            const binary = globalThis.atob(cleaned);
            const bytes = new Uint8Array(binary.length);
            for (let i = 0; i < binary.length; i++)
                bytes[i] = binary.charCodeAt(i);
            return new TextDecoder().decode(bytes);
        }
        const buf = globalThis.Buffer;
        if (buf && typeof buf.from === "function")
            return buf.from(cleaned, "base64").toString("utf8");
        throw new Error("Base64 decoding not available in this environment");
    }
    base64Encode(value) {
        if (typeof globalThis.btoa === "function")
            return globalThis.btoa(value);
        const buf = globalThis.Buffer;
        if (buf && typeof buf.from === "function")
            return buf.from(value, "utf8").toString("base64");
        throw new Error("Base64 encoding not available in this environment");
    }
    ensureOk(status, message) {
        if (status >= 200 && status < 300)
            return;
        if (status === 401)
            throw new WorkspaceBackendError("AUTH_FAILED", message);
        if (status === 403)
            throw new WorkspaceBackendError("FORBIDDEN", message);
        if (status === 404)
            throw new WorkspaceBackendError("NOT_FOUND", message);
        if (status === 409)
            throw new WorkspaceBackendError("CONFLICT", message);
        if (status === 429)
            throw new WorkspaceBackendError("RATE_LIMITED", message);
        throw new WorkspaceBackendError("UNKNOWN", message);
    }
    getCommitMessage(queryId, options, isNew) {
        var _a;
        if ((_a = options === null || options === void 0 ? void 0 : options.message) === null || _a === void 0 ? void 0 : _a.trim())
            return options.message.trim();
        return isNew ? `Add ${queryId}` : `Update ${queryId}`;
    }
    getDeleteMessage(queryId) {
        return `Delete ${queryId}`;
    }
}
//# sourceMappingURL=BaseGitProviderClient.js.map