// Copyright (c) 2025 Patrick Motsch
// All rights reserved.
//
// Persistence is per (mandateId, instanceId): switching to a different mandate
// or instance must remount the editor page so its internal state (loaded
// workflow, currentWorkflowId, …) is reset and saves go to the right tenant.

import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, screen, act } from '@testing-library/react';
import { MemoryRouter, useNavigate } from 'react-router-dom';

const _mountCount = { value: 0 };

vi.mock('./GraphicalEditorPage', () => ({
  GraphicalEditorPage: ({ persistentMandateId, persistentInstanceId }: { persistentMandateId?: string; persistentInstanceId?: string }) => {
    React.useEffect(() => {
      _mountCount.value += 1;
    }, []);
    return <div data-testid="ge-page">{persistentMandateId}::{persistentInstanceId}</div>;
  },
}));

import { GraphicalEditorKeepAlive } from './GraphicalEditorKeepAlive';

let _navigateTo: ((path: string) => void) | null = null;
const _NavCapture: React.FC = () => {
  _navigateTo = useNavigate();
  return null;
};

function _renderHarness(initialPath: string) {
  return render(
    <MemoryRouter initialEntries={[initialPath]}>
      <_NavCapture />
      <GraphicalEditorKeepAlive isVisible />
    </MemoryRouter>,
  );
}

function _navigate(path: string) {
  act(() => {
    _navigateTo?.(path);
  });
}

describe('GraphicalEditorKeepAlive — persistence per (mandate, instance)', () => {
  it('remounts the page when the mandate changes', () => {
    _mountCount.value = 0;
    _renderHarness('/mandates/mA/graphicalEditor/iA/editor');
    expect(_mountCount.value).toBe(1);
    expect(screen.getByTestId('ge-page').textContent).toBe('mA::iA');

    _navigate('/mandates/mB/graphicalEditor/iA/editor');

    expect(_mountCount.value).toBe(2);
    expect(screen.getByTestId('ge-page').textContent).toBe('mB::iA');
  });

  it('remounts the page when the instance changes', () => {
    _mountCount.value = 0;
    _renderHarness('/mandates/mA/graphicalEditor/iA/editor');
    expect(_mountCount.value).toBe(1);

    _navigate('/mandates/mA/graphicalEditor/iZ/editor');

    expect(_mountCount.value).toBe(2);
    expect(screen.getByTestId('ge-page').textContent).toBe('mA::iZ');
  });

  it('does NOT remount when the route stays on the same (mandate, instance)', () => {
    _mountCount.value = 0;
    _renderHarness('/mandates/mA/graphicalEditor/iA/editor');
    expect(_mountCount.value).toBe(1);

    _navigate('/mandates/mA/graphicalEditor/iA/editor');

    expect(_mountCount.value).toBe(1);
  });

  it('keeps the cached page mounted (no remount) when the user navigates AWAY and BACK to the same scope', () => {
    _mountCount.value = 0;
    _renderHarness('/mandates/mA/graphicalEditor/iA/editor');
    expect(_mountCount.value).toBe(1);

    // Away to a non-editor route: the regex match fails, refs keep their
    // previous values — the cached page must not remount.
    _navigate('/admin/languages');
    expect(_mountCount.value).toBe(1);
    expect(screen.getByTestId('ge-page').textContent).toBe('mA::iA');

    // Back to the same (mandate, instance) — still no remount.
    _navigate('/mandates/mA/graphicalEditor/iA/editor');
    expect(_mountCount.value).toBe(1);
  });
});
