"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.search = void 0;
const tslib_1 = require("tslib");
const helpers_1 = require("../helpers");
const constants_1 = require("../constants");
const axiosUtil_1 = require("../utils/axiosUtil");
const search = (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    const { query, env = constants_1.default.ENV.PROD, page = constants_1.default.PAGINATION.INITIAL_PAGE, limit = constants_1.default.PAGINATION.LIMIT, filter, tag, oldFormat = true, } = options || {};
    if (!query)
        throw Error('"query" not provided!');
    const API_BASE_URL = (0, helpers_1.getAPIBaseUrls)(env);
    const apiEndpoint = `${API_BASE_URL}/v1/channels/search/`;
    const queryObj = Object.assign(Object.assign({ page, limit: (0, helpers_1.getLimit)(limit), query }, (tag && { tag })), (filter && { filter }));
    const requestUrl = `${apiEndpoint}?${(0, helpers_1.getQueryParams)(queryObj)}`;
    return (0, axiosUtil_1.axiosGet)(requestUrl)
        .then((response) => {
        const channels = response.data.channels;
        const itemCount = response.data.itemCount || channels.length;
        const formattedResponse = {
            itemCount,
            result: channels,
        };
        if (typeof oldFormat !== 'undefined' && oldFormat) {
            return channels; // Old format: return array directly
        }
        return formattedResponse; // New format: return {itemCount, result}
    })
        .catch((err) => {
        console.error(`[Push SDK] - API ${requestUrl}: `, err);
    });
});
exports.search = search;
//# sourceMappingURL=search.js.map