/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as shared from "../sdk/models/shared/index.js";
import { HTTPClient } from "./http.js";
import { Logger } from "./logger.js";
import { RetryConfig } from "./retries.js";
import { Params, pathToFunc } from "./url.js";

/**
 * Contains the list of servers available to the SDK
 */
export const ServerList = [
  "https://{IP-description}.{identifier}.plex.direct:{port}",
  "{protocol}://{host}:{port}",
  "https://{full_server_url}",
] as const;

export type SDKOptions = {
  token?: string | (() => Promise<string>) | undefined;

  /**
   * Allows setting the accepts parameter for all supported operations
   */
  accepts?: shared.Accepts | undefined;

  /**
   * Allows setting the clientIdentifier parameter for all supported operations
   */
  clientIdentifier?: string | undefined;

  /**
   * Allows setting the product parameter for all supported operations
   */
  product?: string | undefined;

  /**
   * Allows setting the version parameter for all supported operations
   */
  version?: string | undefined;

  /**
   * Allows setting the platform parameter for all supported operations
   */
  platform?: string | undefined;

  /**
   * Allows setting the platformVersion parameter for all supported operations
   */
  platformVersion?: string | undefined;

  /**
   * Allows setting the device parameter for all supported operations
   */
  device?: string | undefined;

  /**
   * Allows setting the model parameter for all supported operations
   */
  model?: string | undefined;

  /**
   * Allows setting the deviceVendor parameter for all supported operations
   */
  deviceVendor?: string | undefined;

  /**
   * Allows setting the deviceName parameter for all supported operations
   */
  deviceName?: string | undefined;

  /**
   * Allows setting the marketplace parameter for all supported operations
   */
  marketplace?: string | undefined;

  httpClient?: HTTPClient;
  /**
   * Allows overriding the default server used by the SDK
   */
  serverIdx?: number | undefined;
  /**
   * Sets the identifier variable for url substitution
   */
  identifier?: string | undefined;
  /**
   * Sets the IP-description variable for url substitution
   */
  ipDescription?: string | undefined;
  /**
   * Sets the port variable for url substitution
   */
  port?: string | undefined;
  /**
   * Sets the protocol variable for url substitution
   */
  protocol?: string | undefined;
  /**
   * Sets the host variable for url substitution
   */
  host?: string | undefined;
  /**
   * Sets the full_server_url variable for url substitution
   */
  fullServerUrl?: string | undefined;
  /**
   * Allows overriding the default server URL used by the SDK
   */
  serverURL?: string | undefined;
  /**
   * Allows overriding the default user agent used by the SDK
   */
  userAgent?: string | undefined;
  /**
   * Allows overriding the default retry config used by the SDK
   */
  retryConfig?: RetryConfig;
  timeoutMs?: number;
  debugLogger?: Logger;
};

export function serverURLFromOptions(options: SDKOptions): URL | null {
  let serverURL = options.serverURL;

  const serverParams: Params[] = [
    {
      "identifier": options.identifier ?? "0123456789abcdef0123456789abcdef",
      "IP-description": options.ipDescription ?? "1-2-3-4",
      "port": options.port ?? "32400",
    },
    {
      "protocol": options.protocol ?? "http",
      "host": options.host ?? "localhost",
      "port": options.port ?? "32400",
    },
    {
      "full_server_url": options.fullServerUrl ?? "http://localhost:32400",
    },
  ];
  let params: Params = {};

  if (!serverURL) {
    const serverIdx = options.serverIdx ?? 0;
    if (serverIdx < 0 || serverIdx >= ServerList.length) {
      throw new Error(`Invalid server index ${serverIdx}`);
    }
    serverURL = ServerList[serverIdx] || "";
    params = serverParams[serverIdx] || {};
  }

  const u = pathToFunc(serverURL)(params);
  return new URL(u);
}

export const SDK_METADATA = {
  language: "typescript",
  openapiDocVersion: "1.1.1",
  sdkVersion: "0.44.0",
  genVersion: "2.767.2",
  userAgent: "speakeasy-sdk/typescript 0.44.0 2.767.2 1.1.1 @lukehagar/plexjs",
} as const;
