/**
 * Base class for workspace-scoped resources.
 *
 * Uses the openapi-fetch client (with auth + error + retry middleware)
 * injected from AmigoClient. Resources call typed HTTP methods that
 * flow through the middleware chain automatically.
 */
import { applyPlatformRequestOptions } from '../core/openapi-client.js';
import { mergeRequestOptions, mergeScopedRequestOptions, } from '../core/request-options.js';
import { extractData } from '../core/utils.js';
const scopedClientState = new WeakMap();
export class WorkspaceScopedResource {
    client;
    workspaceId;
    constructor(client, workspaceId) {
        this.client = client;
        this.workspaceId = workspaceId;
    }
    withOptions(options) {
        const ResourceCtor = this.constructor;
        return new ResourceCtor(scopePlatformClient(this.client, options), this.workspaceId);
    }
    async *iteratePaginatedList(fetchPage, params) {
        let nextParams = params;
        while (true) {
            const page = await fetchPage(nextParams);
            for (const item of page.items ?? []) {
                yield item;
            }
            if (!page.has_more ||
                page.continuation_token === null ||
                page.continuation_token === undefined) {
                break;
            }
            nextParams = {
                ...nextParams,
                continuation_token: page.continuation_token,
            };
        }
    }
    async *iterateOffsetPaginatedList(fetchPage, selectItems, params) {
        let nextParams = params;
        while (true) {
            const page = await fetchPage(nextParams);
            for (const item of selectItems(page)) {
                yield item;
            }
            if (!page.has_more || page.next_offset === null || page.next_offset === undefined) {
                break;
            }
            nextParams = { ...nextParams, offset: page.next_offset };
        }
    }
}
export function scopePlatformClient(client, options) {
    const { baseClient, options: existingOptions } = resolveScopedPlatformClient(client);
    const mergedOptions = mergeScopedRequestOptions(existingOptions, options);
    const scopedClient = {
        request: (method, path, init) => baseClient.request(method, path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        GET: (path, init) => baseClient.GET(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        PUT: (path, init) => baseClient.PUT(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        POST: (path, init) => baseClient.POST(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        DELETE: (path, init) => baseClient.DELETE(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        OPTIONS: (path, init) => baseClient.OPTIONS(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        HEAD: (path, init) => baseClient.HEAD(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        PATCH: (path, init) => baseClient.PATCH(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        TRACE: (path, init) => baseClient.TRACE(path, applyPlatformRequestOptions(baseClient, mergeRequestOptions(mergedOptions, init))),
        use: (...middleware) => baseClient.use(...middleware),
        eject: (...middleware) => baseClient.eject(...middleware),
    };
    scopedClientState.set(scopedClient, { baseClient, options: mergedOptions });
    return scopedClient;
}
export function resolveScopedPlatformClient(client) {
    const existing = scopedClientState.get(client);
    return {
        baseClient: existing?.baseClient ?? client,
        options: existing?.options,
    };
}
export { extractData };
//# sourceMappingURL=base.js.map