/**
 * Kalshi Trade API Manual Endpoints
 * Manually defined OpenAPI spec for endpoints being migrated to spec-first approach
 *
 * The version of the OpenAPI document: 3.11.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

/* tslint:disable */
/* eslint-disable */

import type { Configuration } from './configuration';
// Some imports not used depending on template conditions
// @ts-ignore
import type { AxiosPromise, AxiosInstance, AxiosRequestConfig, Method } from 'axios';
import globalAxios from 'axios';
import { KalshiAuth } from './auth';

export const BASE_PATH = "https://api.elections.kalshi.com/trade-api/v2".replace(/\/+$/, "");

/**
 *
 * @export
 */
export const COLLECTION_FORMATS = {
    csv: ",",
    ssv: " ",
    tsv: "\t",
    pipes: "|",
};

/**
 *
 * @export
 * @interface RequestArgs
 */
export interface RequestArgs {
    url: string;
    options: AxiosRequestConfig;
}

/**
 *
 * @export
 * @class BaseAPI
 */
export class BaseAPI {
    protected configuration: Configuration | undefined;
    protected auth: KalshiAuth | undefined;

    constructor(configuration?: Configuration, protected basePath: string = BASE_PATH, protected axios: AxiosInstance = globalAxios) {
        if (configuration) {
            this.configuration = configuration;
            this.basePath = configuration.basePath || this.basePath;

            // Initialize auth if credentials are provided
            if (configuration.apiKey && (configuration.privateKeyPath || configuration.privateKeyPem)) {
                const privateKey = configuration.privateKeyPem ||
                    (configuration.privateKeyPath ? require('fs').readFileSync(configuration.privateKeyPath, 'utf8') : '');
                this.auth = new KalshiAuth(configuration.apiKey, privateKey);
            }
        }

        // Add request interceptor for authentication
        this.axios.interceptors.request.use((config) => {
            if (this.auth && config.url && config.method) {
                // Extract path from URL
                const url = new URL(config.url, this.basePath);
                const path = url.pathname;
                const method = config.method.toUpperCase();

                // Generate and add auth headers
                const authHeaders = this.auth.generateAuthHeaders(method, path);
                Object.assign(config.headers, authHeaders);
            }
            return config;
        });
    }
};

/**
 *
 * @export
 * @class RequiredError
 * @extends {Error}
 */
export class RequiredError extends Error {
    name: "RequiredError" = "RequiredError";
    constructor(public field: string, msg?: string) {
        super(msg);
    }
}
