var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var search_exports = {};
__export(search_exports, {
  buildSearchApiClientMethods: () => buildSearchApiClientMethods
});
module.exports = __toCommonJS(search_exports);
function setNormalizedStringParam(params, key, value) {
  if (typeof value !== "string") {
    return;
  }
  const normalized = value.trim();
  if (normalized.length > 0) {
    params.set(key, normalized);
  }
}
function setNormalizedPositiveIntegerParam(params, key, value, normalizePositiveIntegerInput, errorCode) {
  if (value === void 0) {
    return;
  }
  params.set(key, String(normalizePositiveIntegerInput(value, errorCode)));
}
function appendNormalizedTagParams(params, tags) {
  if (!Array.isArray(tags)) {
    return;
  }
  for (const tag of tags) {
    if (typeof tag !== "string") {
      continue;
    }
    const normalized = tag.trim();
    if (normalized.length > 0) {
      params.append("tag", normalized);
    }
  }
}
function hasSearchScopeFilters(searchRequest) {
  return typeof searchRequest.creatorUsername === "string" && searchRequest.creatorUsername.trim().length > 0 || Array.isArray(searchRequest.tags) && searchRequest.tags.length > 0 || searchRequest.appType !== void 0 || searchRequest.auth !== void 0 || searchRequest.controller !== void 0 || searchRequest.surface !== void 0 || searchRequest.assetType !== void 0 || searchRequest.assetCategory !== void 0 || typeof searchRequest.assetSubcategory === "string" || typeof searchRequest.assetSpec === "string" || typeof searchRequest.assetSpecOwner === "string" || typeof searchRequest.assetSpecName === "string" || searchRequest.packContainsCategory !== void 0 || typeof searchRequest.packContainsSubcategory === "string";
}
function buildSearchApiClientMethods(input) {
  const { request, handleApiError, normalizeSearchModeInput, normalizeSearchKindInput, normalizeSearchAssetTypeInput, normalizePositiveIntegerInput } = input;
  return {
    async search(searchRequest) {
      const query = typeof searchRequest.q === "string" ? searchRequest.q.trim() : "";
      if (!query && !hasSearchScopeFilters(searchRequest)) {
        throw new Error("invalid_search_query");
      }
      const params = new URLSearchParams();
      if (query) {
        params.set("q", query);
      }
      if (searchRequest.mode) {
        params.set("mode", normalizeSearchModeInput(searchRequest.mode));
      }
      if (searchRequest.kind) {
        params.set("kind", normalizeSearchKindInput(searchRequest.kind));
      }
      if (searchRequest.sort) {
        params.set("sort", searchRequest.sort);
      }
      setNormalizedStringParam(params, "creator", searchRequest.creatorUsername);
      setNormalizedPositiveIntegerParam(params, "page", searchRequest.page, normalizePositiveIntegerInput, "invalid_search_page");
      setNormalizedPositiveIntegerParam(params, "pageSize", searchRequest.pageSize, normalizePositiveIntegerInput, "invalid_search_page_size");
      if (searchRequest.appType) {
        params.set("appType", searchRequest.appType);
      }
      setNormalizedStringParam(params, "auth", searchRequest.auth);
      setNormalizedStringParam(params, "controller", searchRequest.controller);
      setNormalizedStringParam(params, "surface", searchRequest.surface);
      if (searchRequest.assetType) {
        params.set("assetType", normalizeSearchAssetTypeInput(searchRequest.assetType));
      }
      if (searchRequest.assetCategory) {
        params.set("assetCategory", searchRequest.assetCategory);
      }
      setNormalizedStringParam(params, "assetSubcategory", searchRequest.assetSubcategory);
      setNormalizedStringParam(params, "assetSpec", searchRequest.assetSpec);
      setNormalizedStringParam(params, "assetSpecOwner", searchRequest.assetSpecOwner);
      setNormalizedStringParam(params, "assetSpecName", searchRequest.assetSpecName);
      if (searchRequest.packContainsCategory) {
        params.set("packContainsCategory", searchRequest.packContainsCategory);
      }
      setNormalizedStringParam(params, "packContainsSubcategory", searchRequest.packContainsSubcategory);
      appendNormalizedTagParams(params, searchRequest.tags);
      if (searchRequest.includeCounts === false) {
        params.set("includeCounts", "false");
      }
      const response = await request({
        method: "GET",
        path: `/search?${params.toString()}`
      });
      if (response.status !== 200) {
        handleApiError(response, "search");
      }
      return response.body;
    },
    async suggestSearch(searchRequest) {
      const query = typeof searchRequest.q === "string" ? searchRequest.q.trim() : "";
      if (!query) {
        throw new Error("invalid_search_query");
      }
      const response = await request({
        method: "GET",
        path: `/search/suggest?q=${encodeURIComponent(query)}`
      });
      if (response.status !== 200) {
        handleApiError(response, "suggest_search");
      }
      return response.body;
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildSearchApiClientMethods
});
