import '@angular/compiler';
import { provideHttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { afterEach, describe, expect, it } from 'vitest';

import { BaseApiService } from './base-api.service';
import { LOADING_MODE } from './global-loading/loading-mode';

class TestApiService extends BaseApiService {
  contextFor() {
    return this.loadingContext();
  }
}

describe('BaseApiService', () => {
  afterEach(() => {
    TestBed.resetTestingModule();
  });

  it('uses global loading by default', () => {
    TestBed.configureTestingModule({ providers: [provideHttpClient()] });
    const service = TestBed.runInInjectionContext(() => new TestApiService());

    expect(service.contextFor().get(LOADING_MODE)).toBe('global');
  });

  it('creates an isolated custom-loading scope', () => {
    TestBed.configureTestingModule({ providers: [provideHttpClient()] });
    const service = TestBed.runInInjectionContext(() => new TestApiService());

    expect(service.withCustomLoading().contextFor().get(LOADING_MODE)).toBe('custom');
    expect(service.contextFor().get(LOADING_MODE)).toBe('global');
  });

  it('creates an isolated scope without a loading indicator', () => {
    TestBed.configureTestingModule({ providers: [provideHttpClient()] });
    const service = TestBed.runInInjectionContext(() => new TestApiService());

    expect(service.withoutLoading().contextFor().get(LOADING_MODE)).toBe('none');
    expect(service.contextFor().get(LOADING_MODE)).toBe('global');
  });
});
