/*!
 * Copyright (c) what3words http://what3words.com
 */
import what3words, { ApiVersion, AutosuggestClient, AvailableLanguagesClient, ConvertTo3waClient, ConvertToCoordinatesClient, fetchTransport, GridSectionClient, } from "@what3words/api";
import { W3W_REGEX } from "./constants";
const api = what3words("", { apiVersion: ApiVersion.Version3 }, { transport: fetchTransport() });
const utils = {
    validAddress: (v) => W3W_REGEX.test(v),
};
/**
 * A what3words client factory that returns a client constructor with sensible defaults
 * @param clientName
 * @param client
 * @returns (apiKey, config, transport) => new client(apiKey, config, transport)
 **/
const initClient = (clientName, client) => {
    // Reference: https://vitaneri.com/posts/introduction-to-generics-in-typescript#creating-a-new-object-within-generics
    return (apiKey, config, transport) => {
        if (!apiKey) {
            console.warn("Use of sdk script credentials is deprecated! Please pass the `api_key` attribute to components. https://developer.what3words.com/tutorial/javascript-autosuggest-component-v4");
            const client = api.clients[clientName];
            const possibleClientApiKey = client.apiKey();
            // check if apiKey is set by verifying return type is a string
            if (typeof possibleClientApiKey === "string") {
                // reassignment should be fine, though it modifies `arguments` object
                apiKey = possibleClientApiKey;
            }
        }
        return new client(apiKey, config, transport);
    };
};
export const what3wordsClients = {
    autosuggest: initClient("autosuggest", AutosuggestClient),
    availableLanguages: initClient("availableLanguages", AvailableLanguagesClient),
    convertTo3wa: initClient("convertTo3wa", ConvertTo3waClient),
    convertToCoordinates: initClient("convertToCoordinates", ConvertToCoordinatesClient),
    gridSection: initClient("gridSection", GridSectionClient),
};
export const sdk = {
    api,
    utils,
};
