import { AuthSdk } from './AuthSdk.js';
import { FilesSdk } from './FilesSdk.js';
import { UsersSdk } from './UsersSdk.js';
import { HistorySdk } from './HistorySdk.js';
import { SharedSdk } from './SharedSdk.js';
import { RevisionsSdk } from './RevisionsSdk.js';
import { TrashSdk } from './TrashSdk.js';
import { ProjectExecutionSdk } from './ProjectExecutionsSdk.js';
import { OrganizationsSdk } from './OrganizationsSdk.js';
import { DataCatalogSdk } from './DataCatalogSdk.js';
import { GroupsSdk } from './GroupsSdk.js';
const baseUriSymbol = Symbol('baseUri');
/**
 * NodeJS API for API Client's net-store module.
 */
export class Sdk {
    basePath;
    #token;
    /**
     * The token to use with requests. Can be overwritten
     * with each API call by providing custom token.
     */
    get token() {
        return this.#token;
    }
    set token(value) {
        this.#token = value;
    }
    /**
     * The user authentication features.
     */
    auth = new AuthSdk(this);
    /**
     * The user files features.
     */
    files = new FilesSdk(this);
    /**
     * @deprecated Use `files` instead.
     */
    file = this.files;
    /**
     * User management features.
     */
    users = new UsersSdk(this);
    /**
     * @deprecated Use `users` instead.
     */
    user = this.users;
    /**
     * The history data.
     */
    history = new HistorySdk(this);
    /**
     * The shared data.
     */
    shared = new SharedSdk(this);
    revisions = new RevisionsSdk(this);
    trash = new TrashSdk(this);
    projectExecution = new ProjectExecutionSdk(this);
    organizations = new OrganizationsSdk(this);
    dataCatalog = new DataCatalogSdk(this);
    groups = new GroupsSdk(this);
    /**
     * When set it limits log output to minimum.
     */
    silent = false;
    [baseUriSymbol] = '';
    /**
     * @returns The base URI to the store.
     */
    get baseUri() {
        return this[baseUriSymbol];
    }
    /**
     * @param value The base URI to the store.
     */
    set baseUri(value) {
        if (!value) {
            throw new Error(`Invalid value for the baseUri`);
        }
        let url;
        try {
            url = new URL(value);
        }
        catch {
            throw new Error(`Invalid baseUri: Not an URL`);
        }
        this[baseUriSymbol] = url.origin;
        const { pathname } = url;
        if (pathname && pathname !== '/') {
            this.basePath = pathname;
        }
        else {
            this.basePath = undefined;
        }
    }
    /**
     * @param baseUri The base URI to the store.
     * @param basePath The base path (the router prefix) for the server.
     */
    constructor(baseUri, basePath) {
        this.basePath = basePath;
        this.baseUri = baseUri;
    }
    /**
     * Creates a full URL for a path.
     * This adds the server's base path and the passed path to the configured base URL.
     * @param path The path to the endpoint, without the base path.
     */
    getUrl(path = '/') {
        if (path.startsWith('http:') || path.startsWith('https:')) {
            return new URL(path);
        }
        const { baseUri, basePath = '' } = this;
        let userPath = path;
        if (basePath && userPath.startsWith(basePath)) {
            userPath = userPath.substring(basePath.length);
        }
        return new URL(`${basePath}${userPath}`, baseUri);
    }
    /**
     * Appends list options to the query parameters.
     * @param url The URL object to the API endpoint
     * @param options The list options, if any
     */
    appendListOptions(url, options = {}) {
        const { searchParams } = url;
        if (options.cursor) {
            searchParams.set('cursor', options.cursor);
        }
        if (typeof options.limit === 'number') {
            searchParams.set('limit', String(options.limit));
        }
        if (options.parent) {
            searchParams.set('parent', options.parent);
        }
        if (options.since) {
            searchParams.set('since', String(options.since));
        }
        if (options.sort) {
            searchParams.set('sort', String(options.sort));
        }
        if (options.order) {
            searchParams.set('order', String(options.order));
        }
        if (options.oid) {
            searchParams.set('oid', String(options.oid));
        }
        if (options.uid) {
            searchParams.set('uid', String(options.uid));
        }
        if (options.filter) {
            const bytes = new TextEncoder().encode(JSON.stringify(options.filter));
            // To be used when Uint8Array.prototype.toBase64() is available.
            // const encoded = bytes.toBase64({ alphabet: 'base64url' })
            const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join('');
            searchParams.set('filter', btoa(binString));
        }
    }
}
//# sourceMappingURL=Sdk.js.map