"use strict";
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT License.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 * Changes may cause incorrect behavior and will be lost if the code is regenerated.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchClient = void 0;
const tslib_1 = require("tslib");
const coreHttpCompat = tslib_1.__importStar(require("@azure/core-http-compat"));
const index_js_1 = require("./operations/index.js");
/** @internal */
class SearchClient extends coreHttpCompat.ExtendedServiceClient {
    endpoint;
    indexName;
    apiVersion;
    /**
     * Initializes a new instance of the SearchClient class.
     * @param endpoint The endpoint URL of the search service.
     * @param indexName The name of the index.
     * @param apiVersion Api Version
     * @param options The parameter options
     */
    constructor(endpoint, indexName, apiVersion, options) {
        if (endpoint === undefined) {
            throw new Error("'endpoint' cannot be null");
        }
        if (indexName === undefined) {
            throw new Error("'indexName' cannot be null");
        }
        if (apiVersion === undefined) {
            throw new Error("'apiVersion' cannot be null");
        }
        // Initializing default values for options
        if (!options) {
            options = {};
        }
        const defaults = {
            requestContentType: "application/json; charset=utf-8",
        };
        const packageDetails = `azsdk-js-search-documents/12.2.0`;
        const userAgentPrefix = options.userAgentOptions && options.userAgentOptions.userAgentPrefix
            ? `${options.userAgentOptions.userAgentPrefix} ${packageDetails}`
            : `${packageDetails}`;
        const optionsWithDefaults = {
            ...defaults,
            ...options,
            userAgentOptions: {
                userAgentPrefix,
            },
            endpoint: options.endpoint ??
                options.baseUri ??
                "{endpoint}/indexes('{indexName}')",
        };
        super(optionsWithDefaults);
        // Parameter assignments
        this.endpoint = endpoint;
        this.indexName = indexName;
        this.apiVersion = apiVersion;
        this.documents = new index_js_1.DocumentsImpl(this);
        this.addCustomApiVersionPolicy(apiVersion);
    }
    /** A function that adds a policy that sets the api-version (or equivalent) to reflect the library version. */
    addCustomApiVersionPolicy(apiVersion) {
        if (!apiVersion) {
            return;
        }
        const apiVersionPolicy = {
            name: "CustomApiVersionPolicy",
            async sendRequest(request, next) {
                const param = request.url.split("?");
                if (param.length > 1) {
                    const newParams = param[1].split("&").map((item) => {
                        if (item.indexOf("api-version") > -1) {
                            return "api-version=" + apiVersion;
                        }
                        else {
                            return item;
                        }
                    });
                    request.url = param[0] + "?" + newParams.join("&");
                }
                return next(request);
            },
        };
        this.pipeline.addPolicy(apiVersionPolicy);
    }
    documents;
}
exports.SearchClient = SearchClient;
//# sourceMappingURL=searchClient.js.map