import { s as search } from './search-ba83fe91.js';
import { g as KlevuTypeOfRecord } from './klevuFetch-4a3f7817.js';
import { a as KlevuConfig } from './klevu-init-0abe4d30.js';

/**
 * Create a trending products search query. Id for this query is `trendingProducts`
 *
 * @category Query
 * @param options {@link search}
 * @returns See {@link KlevuFetchFunction}
 *
 * @example Simple example
 * ```
 * const result = await KlevuFetch(
 *  trendingProducts()
 * )
 *
 * console.log(result.getQueries("trendingProducts").records)
 * ```
 */
function trendingProducts(options, ...modifiers) {
    return search("*", Object.assign({ id: "trendingProducts", typeOfRecords: [KlevuTypeOfRecord.Product] }, options), ...modifiers);
}

var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
/**
 * This function is to be called to get the url to be used
 * in search for image search or to be passed to the imageSearch
 * query function
 * @param image The image object to upload
 * @returns image url to be passed to search
 */
function KlevuUploadImageForSearch(image) {
    return __awaiter(this, void 0, void 0, function* () {
        const form = new FormData();
        form.append("image", image, image.name || KlevuConfig.getDefault().apiKey + ".jpg");
        const imageUrlResponse = yield fetch(`https://api.ksearchnet.com/image/store/${KlevuConfig.getDefault().apiKey}`, { method: "POST", body: form });
        if (imageUrlResponse.status !== 200) {
            console.warn("Failed to upload image: " + imageUrlResponse.status);
            throw new Error("Failed to upload image");
        }
        const { url } = yield imageUrlResponse.json();
        return url;
    });
}

/**
 * Used to replace any amount of %s tokens with array of strings to concatenated string.
 * `hello %s %s %s` with values ['1', 'world', '2'] will lead to `hello 1 world 2`.
 *
 * If there isn't enough array values then %s will be left to string.
 *
 * @param string
 * @param values
 * @returns
 */
function stringConcat(string, values) {
  let copy = string;
  for (const v of values) {
    copy = copy.replace("%s", v);
  }
  return copy;
}

export { KlevuUploadImageForSearch as K, stringConcat as s, trendingProducts as t };

//# sourceMappingURL=stringConcat-5973198a.js.map