// #region BulkReplaceModalTests [C:2] [TYPE Module] [SEMANTICS test, translate, bulk, replace, find-replace]
// @BRIEF Contract-focused unit tests for BulkReplaceModal.svelte component.
// @LAYER Tests
// @RELATION BINDS_TO -> [EXT:spec:BulkReplaceModal:Component]
// @TEST_CONTRACT: BulkReplaceModal -> configuring, preview, confirm, apply flow
// @TEST_EDGE: configuring -> Find/replace inputs, regex toggle, language selector
// @TEST_EDGE: previewing -> Preview table showing before/after
// @TEST_EDGE: confirming -> Confirmation dialog with submit-to-dict checkbox
// @TEST_EDGE: applied -> Success state

import { describe, it, expect, vi } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const COMPONENT_PATH = path.resolve(
	process.cwd(),
	'src/lib/components/translate/BulkReplaceModal.svelte'
);

describe('BulkReplaceModal Component', () => {
	it('component file exists', () => {
		const exists = fs.existsSync(COMPONENT_PATH);
		expect(exists).toBe(true);
	});

	it('contains required UX state tags and contract annotations', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

		expect(source).toContain('@UX_STATE closed');
		expect(source).toContain('@UX_STATE configuring');
		expect(source).toContain('@UX_STATE previewing');
		expect(source).toContain('@UX_STATE preview_error');
		expect(source).toContain('@UX_STATE confirming');
		expect(source).toContain('@UX_STATE applying');
		expect(source).toContain('@UX_STATE applied');
		expect(source).toContain('@UX_STATE apply_error');
		expect(source).toContain('bulkFindReplace');
		expect(source).toContain('bulkReplacePreview');
	});

	it('accepts show, runId, targetLanguages, onClose, onApplied props', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain('show');
		expect(source).toContain('runId');
		expect(source).toContain('targetLanguages');
		expect(source).toContain('onClose');
		expect(source).toContain('onApplied');
	});

	it('has find pattern input with regex toggle', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain('bulk_replace?.find_pattern');
		expect(source).toContain('findPattern');
		expect(source).toContain('isRegex');
		expect(source).toContain('bulk_replace?.regex');
	});

	it('has replacement text input', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain('bulk_replace?.replace_with');
		expect(source).toContain('replacementText');
	});

	it('has target language selector', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain('bulk_replace?.target_language');
		expect(source).toContain('targetLanguage');
	});

	it('has preview table showing before/after values', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain('bulk_replace?.preview_count');
		expect(source).toContain('bulk_replace?.table_before');
		expect(source).toContain('bulk_replace?.table_after');
		expect(source).toContain('bulk_replace?.apply_changes');
	});

	it('has confirmation dialog with submit-to-dictionary checkbox', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain('confirming');
		expect(source).toContain('submitToDict');
		expect(source).toContain('usageNotes');
	});

	it('implements all UX states in template', () => {
		const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');
		expect(source).toContain("uxState === 'configuring'");
		expect(source).toContain("uxState === 'previewing'");
		expect(source).toContain("uxState === 'preview_error'");
		expect(source).toContain("uxState === 'confirming'");
		expect(source).toContain("uxState === 'applying'");
		expect(source).toContain("uxState === 'applied'");
		expect(source).toContain("uxState === 'apply_error'");
	});
});

// #endregion BulkReplaceModalTests
