/**
 * Consolidated API Client (auto-generated, strict types, minimal, DRY)
 * Generated at: {{ generation_time }}
 * DO NOT EDIT - This file is automatically generated
 */

// Import group APIs
{% if zones_by_group.cfg %}
import { API as CfgAPI } from './cfg';
{% endif %}
{% if zones_by_group.custom %}
import { API as CustomAPI } from './custom';
{% endif %}

// Re-export group modules
{% if zones_by_group.cfg %}
export * from './cfg';
{% endif %}
{% if zones_by_group.custom %}
export * from './custom';
{% endif %}

export const TOKEN_KEY = "auth_token";
export const REFRESH_TOKEN_KEY = "refresh_token";

function makeEndpoints<T extends Record<string, any>, C>(
  endpoints: T,
  client: C
): {
  [K in keyof T]: T[K] extends (options: infer O) => infer R
    ? (options?: Omit<O, 'client'>) => R
    : never;
} {
  const result = {} as any;
  (Object.keys(endpoints) as Array<keyof T>).forEach((key) => {
    const fn = endpoints[key];
    if (typeof fn === 'function') {
      result[key] = (options = {}) => fn({ ...options, client });
    }
  });
  return result as {
    [K in keyof T]: T[K] extends (options: infer O) => infer R
      ? (options?: Omit<O, 'client'>) => R
      : never;
  };
}

/**
 * Consolidated API class that combines CFG and Custom APIs
 */
export class API {
  private apiUrl: string;

  {% if zones_by_group.cfg %}
  private cfgApi: CfgAPI;
  {% endif %}
  {% if zones_by_group.custom %}
  private customApi: CustomAPI;
  {% endif %}

  // CFG zone endpoints
  {% for zone in zones_by_group.cfg %}
  public {{ camelcase(zone) }}!: CfgAPI['{{ camelcase(zone) }}'];
  {% endfor %}

  // Custom zone endpoints
  {% for zone in zones_by_group.custom %}
  public {{ camelcase(zone) }}!: CustomAPI['{{ camelcase(zone) }}'];
  {% endfor %}

  constructor(apiUrl: string) {
    this.apiUrl = apiUrl;

    {% if zones_by_group.cfg %}
    this.cfgApi = new CfgAPI(apiUrl);
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi = new CustomAPI(apiUrl);
    {% endif %}

    this._initClients();
  }

  _initClients() {
    // Map CFG endpoints
    {% for zone in zones_by_group.cfg %}
    this.{{ camelcase(zone) }} = this.cfgApi.{{ camelcase(zone) }};
    {% endfor %}

    // Map Custom endpoints
    {% for zone in zones_by_group.custom %}
    this.{{ camelcase(zone) }} = this.customApi.{{ camelcase(zone) }};
    {% endfor %}
  }

  /**
   * Set custom headers for API requests
   * @param headers - Object with header key-value pairs
   */
  setHeaders(headers: Record<string, string>) {
    {% if zones_by_group.cfg %}
    this.cfgApi.setHeaders(headers);
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi.setHeaders(headers);
    {% endif %}
  }

  /**
   * Set API key for authentication
   * @param apiKey - The API key to use
   */
  setApiKey(apiKey: string) {
    {% if zones_by_group.cfg %}
    this.cfgApi.setApiKey(apiKey);
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi.setApiKey(apiKey);
    {% endif %}
  }

  /**
   * Clear all custom headers
   */
  clearHeaders() {
    {% if zones_by_group.cfg %}
    this.cfgApi.clearHeaders();
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi.clearHeaders();
    {% endif %}
  }

  /**
   * Get current custom headers
   */
  getHeaders(): Record<string, string> {
    {% if zones_by_group.cfg %}
    return this.cfgApi.getHeaders();
    {% elif zones_by_group.custom %}
    return this.customApi.getHeaders();
    {% else %}
    return {};
    {% endif %}
  }

  getToken() {
    {% if zones_by_group.cfg %}
    return this.cfgApi.getToken();
    {% elif zones_by_group.custom %}
    return this.customApi.getToken();
    {% else %}
    return null;
    {% endif %}
  }

  getRefreshToken() {
    {% if zones_by_group.cfg %}
    return this.cfgApi.getRefreshToken();
    {% elif zones_by_group.custom %}
    return this.customApi.getRefreshToken();
    {% else %}
    return null;
    {% endif %}
  }

  setToken(token: string, refreshToken?: string) {
    {% if zones_by_group.cfg %}
    this.cfgApi.setToken(token, refreshToken);
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi.setToken(token, refreshToken);
    {% endif %}
    this._initClients();
  }

  clearTokens() {
    {% if zones_by_group.cfg %}
    this.cfgApi.clearTokens();
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi.clearTokens();
    {% endif %}
    this._initClients();
  }

  isAuthenticated() {
    {% if zones_by_group.cfg %}
    return this.cfgApi.isAuthenticated();
    {% elif zones_by_group.custom %}
    return this.customApi.isAuthenticated();
    {% else %}
    return false;
    {% endif %}
  }

  setApiUrl(url: string) {
    this.apiUrl = url;
    {% if zones_by_group.cfg %}
    this.cfgApi.setApiUrl(url);
    {% endif %}
    {% if zones_by_group.custom %}
    this.customApi.setApiUrl(url);
    {% endif %}
    this._initClients();
  }

  getApiUrl() {
    return this.apiUrl;
  }
}

// Export the class for manual instantiation
export default API; 