import { getOrganizationEndpoint, PlatformClient, } from '../platform-client.js';
import { buildAPIResponseFromErrorOrThrow } from '../search/search-api-error-response.js';
import { getRequestOptions, } from './common/request.js';
import { buildRecommendationsRequest, } from './recommendations/recommendations-request.js';
import { getQuerySuggestRequestOptions, } from './search/query-suggest/query-suggest-request.js';
export const isErrorResponse = (r) => {
    return r.error !== undefined;
};
export class CommerceAPIClient {
    options;
    constructor(options) {
        this.options = options;
    }
    async getProductListing(req) {
        return this.query({
            ...getRequestOptions(req, 'listing'),
            ...this.options,
        });
    }
    async search(req) {
        const requestOptions = getRequestOptions(req, 'search');
        return this.query({
            ...requestOptions,
            requestParams: {
                ...requestOptions.requestParams,
                query: req?.query,
            },
            ...this.options,
        });
    }
    async getRecommendations(req) {
        return this.query({
            ...buildRecommendationsRequest(req, 'recommendations'),
            ...this.options,
        });
    }
    async productSuggestions(req) {
        const requestOptions = getRequestOptions(req, 'search/productSuggest');
        return this.query({
            ...requestOptions,
            requestParams: {
                ...requestOptions.requestParams,
                query: req?.query,
            },
            ...this.options,
        });
    }
    async querySuggest(req) {
        const requestOptions = getQuerySuggestRequestOptions(req);
        return this.query({
            ...requestOptions,
            requestParams: {
                ...requestOptions.requestParams,
                query: req?.query,
            },
            ...this.options,
        });
    }
    async facetSearch(req, type) {
        const requestOptions = getRequestOptions(req, 'facet');
        return this.query({
            ...requestOptions,
            url: `${requestOptions.url}?type=${type}`,
            requestParams: {
                ...requestOptions.requestParams,
                facetId: req?.facetId,
                facetQuery: req?.facetQuery,
                query: req?.query,
                numberOfValues: req?.numberOfValues,
            },
            ...this.options,
        });
    }
    // eslint-disable-next-line @cspell/spellchecker
    // TODO: CAPI-867 - Use Commerce API's equivalent of the /plan endpoint when it becomes available.
    async plan(req) {
        const requestOptions = getRequestOptions(req, 'search');
        return this.query({
            ...requestOptions,
            requestParams: {
                ...requestOptions.requestParams,
                query: req?.query,
            },
            ...this.options,
        });
    }
    async query(options) {
        const response = await PlatformClient.call(options);
        if (response instanceof Error) {
            return buildAPIResponseFromErrorOrThrow(response);
        }
        const body = await response.json();
        return response.ok
            ? { success: body }
            : { error: body };
    }
}
export function getCommerceApiBaseUrl(organizationId, environment = 'prod') {
    const platformEndpoint = getOrganizationEndpoint(organizationId, environment);
    return `${platformEndpoint}/rest/organizations/${organizationId}/commerce/v2`;
}
