"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TagsService = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _http = require("@wix/bex-utils/@wix/ambassador-os-tags-v1-tag/http");
var _types = require("@wix/bex-utils/@wix/ambassador-os-tags-v1-tag/types");
class TagsService {
  constructor({
    container
  }) {
    (0, _defineProperty2.default)(this, "container", void 0);
    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(_http.listTagsForAccount, _http.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) {
    var _pagingMetadata$curso;
    const fn = this.getContextFunction(_http.queryTagsForAccount, _http.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 == null || (_pagingMetadata$curso = pagingMetadata.cursors) == null ? void 0 : _pagingMetadata$curso.next,
      hasNext: pagingMetadata == null ? void 0 : pagingMetadata.hasNext,
      total: pagingMetadata == null ? void 0 : pagingMetadata.total
    };
  }
  async fetchTagsByIds({
    fqdn,
    tagIds,
    exposure = _types.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((0, _http.createTag)(request)), errorCodesMap);
      return tag;
    } else {
      const {
        data: {
          tag
        }
      } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request((0, _http.createTagForAccount)(request)), errorCodesMap);
      return tag;
    }
  }
  async updateTag(request) {
    const fn = this.getContextFunction(_http.updateTagForAccount, _http.updateTag);
    const {
      data: {
        tag
      }
    } = await this.container.errorHandler.withErrorHandler(() => this.httpClient.request(fn(request)), {});
    return tag;
  }
  async deleteTag(request) {
    const fn = this.getContextFunction(_http.deleteTagForAccount, _http.deleteTag);
    return this.container.errorHandler.withErrorHandler(() => this.httpClient.request(fn(request)), {});
  }
}
exports.TagsService = TagsService;
//# sourceMappingURL=tagsService.js.map