import assert from "node:assert/strict";
import { afterEach, describe, it } from "node:test";
import { DEFAULT_DESKTOP_LAYOUT_SETTINGS } from "../apps/geolibre-desktop/src/hooks/useDesktopSettings";
import { layoutOptionsFromLocation } from "../apps/geolibre-desktop/src/hooks/useLayoutOptions";

const originalWindow = (globalThis as { window?: unknown }).window;

function withSearch(search: string): void {
  (globalThis as { window?: unknown }).window = {
    location: { search },
  };
}

afterEach(() => {
  if (originalWindow === undefined) {
    delete (globalThis as { window?: unknown }).window;
  } else {
    (globalThis as { window?: unknown }).window = originalWindow;
  }
});

describe("layoutOptionsFromLocation", () => {
  it("keeps all chrome visible without query params", () => {
    withSearch("");
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.toolbarVisible, true);
    assert.equal(options.statusBarVisible, true);
    assert.equal(options.layerPanelVisible, true);
    assert.equal(options.stylePanelVisible, true);
    assert.equal(options.attributePanelVisible, true);
    assert.equal(options.panelsHidden, false);
    assert.equal(options.panelsCollapsed, false);
  });

  it("hides every chrome element when maponly is set as a bare flag", () => {
    withSearch("?maponly");
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.toolbarVisible, false);
    assert.equal(options.statusBarVisible, false);
    assert.equal(options.layerPanelVisible, false);
    assert.equal(options.stylePanelVisible, false);
    assert.equal(options.attributePanelVisible, false);
    // Gates the whole side dock (plugin/Browser panels + shared rail), not just
    // the built-in Layers/Style panels.
    assert.equal(options.panelsHidden, true);
  });

  it("accepts truthy maponly values", () => {
    for (const value of ["true", "1", "yes", "on"]) {
      withSearch(`?maponly=${value}`);
      const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
      assert.equal(options.toolbarVisible, false, `maponly=${value}`);
      assert.equal(options.statusBarVisible, false, `maponly=${value}`);
      assert.equal(options.layerPanelVisible, false, `maponly=${value}`);
      assert.equal(options.stylePanelVisible, false, `maponly=${value}`);
      assert.equal(options.attributePanelVisible, false, `maponly=${value}`);
    }
  });

  it("returns defaults when window is undefined (SSR)", () => {
    delete (globalThis as { window?: unknown }).window;
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.toolbarVisible, true);
    assert.equal(options.statusBarVisible, true);
    assert.equal(options.compact, false);
    assert.equal(options.layerPanelVisible, true);
    assert.equal(options.stylePanelVisible, true);
    assert.equal(options.attributePanelVisible, true);
    assert.equal(options.panelsHidden, false);
  });

  it("ignores maponly with a non-truthy value", () => {
    withSearch("?maponly=false");
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.toolbarVisible, true);
    assert.equal(options.statusBarVisible, true);
    assert.equal(options.layerPanelVisible, true);
    assert.equal(options.panelsHidden, false);
  });

  it("leaves the toolbar and status bar visible for panels=none", () => {
    withSearch("?panels=none");
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.toolbarVisible, true);
    assert.equal(options.statusBarVisible, true);
    assert.equal(options.layerPanelVisible, false);
    assert.equal(options.stylePanelVisible, false);
    assert.equal(options.attributePanelVisible, false);
    assert.equal(options.panelsHidden, true);
  });

  it("starts side panels collapsed without hiding their rails", () => {
    withSearch("?panels=collapsed");
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.toolbarVisible, true);
    assert.equal(options.layerPanelVisible, true);
    assert.equal(options.stylePanelVisible, true);
    assert.equal(options.attributePanelVisible, true);
    assert.equal(options.panelsHidden, false);
    assert.equal(options.panelsCollapsed, true);
  });

  it("provides read-only viewer chrome between the full app and maponly", () => {
    withSearch("?layout=viewer");
    const options = layoutOptionsFromLocation(DEFAULT_DESKTOP_LAYOUT_SETTINGS);
    assert.equal(options.viewer, true);
    assert.equal(options.compact, true);
    assert.equal(options.toolbarVisible, true);
    assert.equal(options.statusBarVisible, true);
    assert.equal(options.layerPanelVisible, true);
    assert.equal(options.stylePanelVisible, false);
    assert.equal(options.attributePanelVisible, false);
    assert.equal(options.panelsHidden, false);
  });

  it("forces the viewer layer legend on despite a persisted hidden setting", () => {
    withSearch("?layout=viewer");
    const options = layoutOptionsFromLocation({
      ...DEFAULT_DESKTOP_LAYOUT_SETTINGS,
      layerPanelVisible: false,
    });
    assert.equal(options.layerPanelVisible, true);
  });
});
