var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { WorkspaceBackendError } from "./errors";
import { normalizeQueryFilename } from "../normalizeQueryFilename";
export default class GitWorkspaceBackend {
    constructor(config, client) {
        this.config = config;
        this.client = client;
        this.type = "git";
    }
    missingClientError() {
        return new WorkspaceBackendError("UNKNOWN", `No GitProviderClient configured for remote '${this.config.remoteUrl}'. Supported providers: GitHub, GitLab, Bitbucket Cloud (bitbucket.org), and Gitea. For self-hosted/enterprise instances, set git workspace 'provider' and/or 'apiBaseUrl'.`);
    }
    validateAccess() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.validateAccess(this.config);
        });
    }
    listFolder(_folderId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.listFolder(this.config, _folderId);
        });
    }
    readQuery(_queryId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.readQuery(this.config, _queryId);
        });
    }
    writeQuery(_queryId, _queryText, _options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.writeQuery(this.config, _queryId, _queryText, _options);
        });
    }
    listVersions(_queryId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.listVersions(this.config, _queryId);
        });
    }
    readVersion(_queryId, _versionId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.readVersion(this.config, _queryId, _versionId);
        });
    }
    deleteQuery(_queryId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            return this.client.deleteQuery(this.config, _queryId);
        });
    }
    renameQuery(queryId, newLabel) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.client)
                throw this.missingClientError();
            const next = (newLabel || "").trim();
            if (!next)
                throw new WorkspaceBackendError("UNKNOWN", "New name is required");
            const parts = queryId.split("/").filter(Boolean);
            const oldFilename = parts.pop() || queryId;
            const folderPrefix = parts.join("/");
            const safe = next.replace(/[\\/]/g, "-");
            const newFilename = normalizeQueryFilename(safe);
            const newPath = folderPrefix ? `${folderPrefix}/${newFilename}` : newFilename;
            if (newPath === queryId)
                return;
            const read = yield this.client.readQuery(this.config, queryId);
            yield this.client.writeQuery(this.config, newPath, read.queryText, {
                message: `Rename ${oldFilename} to ${newFilename}`,
            });
            yield this.client.deleteQuery(this.config, queryId);
        });
    }
}
//# sourceMappingURL=GitWorkspaceBackend.js.map