import { Injectable } from '@angular/core';
import { environment } from '../../../environments/environment';

export type RuntimeProviderMode = 'mock' | 'api' | 'remote-config';

@Injectable({ providedIn: 'root' })
export class RuntimeProviderStrategyService {
  private isLocalhost(): boolean {
    if (typeof window === 'undefined') {
      return false;
    }

    const host = window.location.hostname.toLowerCase();
    return host === 'localhost' || host === '127.0.0.1' || host === '::1';
  }

  getBootstrapProviderMode(): RuntimeProviderMode {
    if (environment.useMockData) {
      return 'mock';
    }

    // Local dev must use mocked bootstrap while API data can stay production-based.
    if ((environment as any).useMockBootstrapOnLocal === true && this.isLocalhost()) {
      return 'mock';
    }

    return 'api';
  }

  getBackofficeProviderMode(): RuntimeProviderMode {
    if (environment.useMockData) {
      return 'mock';
    }

    return 'api';
  }

  getProductProviderMode(): RuntimeProviderMode {
    if (environment.useMockData) {
      return 'mock';
    }

    return 'api';
  }

  getCategoryProviderMode(): RuntimeProviderMode {
    if (environment.useMockData) {
      return 'mock';
    }

    return 'api';
  }
}
