import { listTags, listTagsForAccount, queryTags, queryTagsForAccount, createTag, createTagForAccount, updateTag, updateTagForAccount, deleteTag, deleteTagForAccount, } from '@wix/bex-utils/@wix/ambassador-os-tags-v1-tag/http';
import { TagExposure, } from '@wix/bex-utils/@wix/ambassador-os-tags-v1-tag/types';
export class TagsService {
    constructor({ container }) {
        this.container = container;
    }
    get httpClient() {
        return this.container.httpClient;
    }
    getContextFunction(accountFn, siteFn) {
        return this.container.contextType === 'ACCOUNT' ? accountFn : siteFn;
    }
    async listTags(request, errorCodesMap, options) {
        const fn = this.getContextFunction(listTagsForAccount, listTags);
        const { data: { tags = [] }, } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request(fn(request)), errorCodesMap, options);
        return tags.filter(({ id, name }) => id && name);
    }
    async queryTags(request, errorCodesMap, options) {
        const fn = this.getContextFunction(queryTagsForAccount, queryTags);
        const { data: { tags = [], pagingMetadata }, } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request(fn(request)), errorCodesMap, options);
        const items = tags.filter(({ id, name }) => id && name);
        return {
            items,
            cursor: pagingMetadata?.cursors?.next,
            hasNext: pagingMetadata?.hasNext,
            total: pagingMetadata?.total,
        };
    }
    async fetchTagsByIds({ fqdn, tagIds, exposure = TagExposure.PRIVATE, pageSize = 100, }, errorCodesMap, options) {
        if (!tagIds.length) {
            return [];
        }
        const items = [];
        let cursor;
        let hasNext = true;
        while (hasNext) {
            const result = await this.queryTags({
                fqdn,
                exposure,
                query: {
                    filter: { id: { $in: tagIds } },
                    cursorPaging: { limit: Math.min(pageSize, tagIds.length), cursor },
                },
            }, errorCodesMap, options);
            items.push(...result.items);
            cursor = result.cursor ?? undefined;
            hasNext = !!result.hasNext && !!cursor;
        }
        return items;
    }
    async createTag(request, errorCodesMap) {
        // We cannot use getContextFunction because createTagForAccount and createTag have different return signatures due to error handling
        if (this.container.contextType === 'SITE') {
            const { data: { tag }, } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request(createTag(request)), errorCodesMap);
            return tag;
        }
        else {
            const { data: { tag }, } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request(createTagForAccount(request)), errorCodesMap);
            return tag;
        }
    }
    async updateTag(request) {
        const fn = this.getContextFunction(updateTagForAccount, updateTag);
        const { data: { tag }, } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request(fn(request)), {});
        return tag;
    }
    async deleteTag(request) {
        const fn = this.getContextFunction(deleteTagForAccount, deleteTag);
        return this.container.errorHandler.withErrorHandler(() => this.httpClient.request(fn(request)), {});
    }
}
//# sourceMappingURL=tagsService.js.map