jest.mock("@turf/turf", () => ({
  along: jest.fn(),
  lineString: jest.fn(),
  length: jest.fn(),
  toMercator: jest.fn(),
}));
jest.mock("ol/format/MVT", () => ({ __esModule: true, default: class MVT {} }));
jest.mock("ol/format/GeoJSON", () => ({ __esModule: true, default: class GeoJSON {} }));
jest.mock("ol/geom", () => ({ Point: class Point {} }));
jest.mock("ol/proj", () => ({ toLonLat: jest.fn() }));
jest.mock("ol/style", () => ({ Icon: class Icon {}, Style: class Style {} }));
jest.mock("ol/source/Vector", () => ({
  __esModule: true,
  default: class VectorSource {
    constructor(readonly options: unknown) {}
  },
}));
jest.mock("ol/source/VectorTile", () => ({
  __esModule: true,
  default: class VectorTileSource {
    constructor(readonly options: unknown) {}
  },
}));

jest.mock("ol/layer/Vector", () => ({
  __esModule: true,
  default: class MockVectorLayer {
    private readonly source: unknown;
    private readonly properties = new Map<string, unknown>();
    constructor(options: any) {
      this.source = options.source;
      Object.entries(options.properties || {}).forEach(([key, value]) =>
        this.properties.set(key, value),
      );
    }
    getSource() { return this.source; }
    get(key: string) { return this.properties.get(key); }
    set(key: string, value: unknown) { this.properties.set(key, value); }
    setStyle() {}
  },
}));
jest.mock("ol/layer/WebGLVectorTile", () => ({
  __esModule: true,
  default: class MockWebGlVectorTileLayer {
    private readonly source: unknown;
    private readonly properties = new Map<string, unknown>();
    constructor(options: any) {
      this.source = options.source;
      Object.entries(options.properties || {}).forEach(([key, value]) =>
        this.properties.set(key, value),
      );
    }
    getSource() { return this.source; }
    get(key: string) { return this.properties.get(key); }
    set(key: string, value: unknown) { this.properties.set(key, value); }
    setStyle() {}
  },
}));

import {
  createOperationalMapResources,
  createOperationalMapSources,
} from "./operationalLayers";

describe("operational map resources", () => {
  it("shares sources while keeping per-map layer instances independent", () => {
    const options = {
      mapUrl: "https://maps.example.test/geoserver",
      workspace: "test_workspace",
      extent: [0, 0, 100, 100] as [number, number, number, number],
    };
    const sources = createOperationalMapSources(options);
    const primary = createOperationalMapResources({ ...options, sources });
    const compare = createOperationalMapResources({ ...options, sources });

    Object.keys(sources).forEach((sourceId) => {
      const key = sourceId as keyof typeof sources;
      expect(primary.sources[key]).toBe(compare.sources[key]);
      expect(primary.layers[key]).not.toBe(compare.layers[key]);
      expect(primary.layers[key].getSource()).toBe(sources[key]);
      expect(compare.layers[key].getSource()).toBe(sources[key]);
    });
  });
});
