import '@angular/compiler';
import { DefaultUrlSerializer } from '@angular/router';
import { describe, expect, it } from 'vitest';

import { Tenant } from './tenant.interface';
import { TenantService } from './tenant.service';
import { TenantUrlSerializer } from './tenant-url.serializer';

describe('TenantUrlSerializer', () => {
  const defaultSerializer = new DefaultUrlSerializer();

  function createSerializer(basePath: string): TenantUrlSerializer {
    const tenantService = {
      getTenant: () => ({ base_path: basePath }) as Tenant,
    } as TenantService;

    return new TenantUrlSerializer(tenantService);
  }

  it('keeps root tenants unchanged', () => {
    const serializer = createSerializer('/');
    const tree = serializer.parse('/producto/123?ref=home');

    expect(defaultSerializer.serialize(tree)).toBe('/producto/123?ref=home');
    expect(serializer.serialize(tree)).toBe('/producto/123?ref=home');
  });

  it('removes the tenant base path when parsing and restores it when serializing', () => {
    const serializer = createSerializer('/desfile');
    const tree = serializer.parse('/desfile/producto/123?ref=home#detalle');

    expect(defaultSerializer.serialize(tree)).toBe('/producto/123?ref=home#detalle');
    expect(serializer.serialize(tree)).toBe('/desfile/producto/123?ref=home#detalle');
  });

  it('maps the tenant base path to the application root', () => {
    const serializer = createSerializer('/desfile/');
    const tree = serializer.parse('/desfile');

    expect(defaultSerializer.serialize(tree)).toBe('/');
    expect(serializer.serialize(tree)).toBe('/desfile');
  });

  it('does not strip partial path segment matches', () => {
    const serializer = createSerializer('/desfile');
    const tree = serializer.parse('/desfile-shop/producto/123');

    expect(defaultSerializer.serialize(tree)).toBe('/desfile-shop/producto/123');
  });
});
