import { Injectable } from '@angular/core';
import type { ElectronApi } from './electron-api.models';
import { getElectronApi } from './get-electron-api';

@Injectable({ providedIn: 'root' })
export class ElectronBridgeService {
  get isAvailable(): boolean {
    return this.getApi() !== null;
  }

  getApi(): ElectronApi | null {
    return getElectronApi();
  }

  requireApi(): ElectronApi {
    const api = this.getApi();

    if (!api) {
      throw new Error('Electron API is not available in this runtime.');
    }

    return api;
  }
}
