import fs from 'node:fs';
import path from 'node:path';

import type { WebviewsDomainContext } from './context.mts';

import {
	createEmptyCatalogFile,
	createEmptyReconciliationReportFile,
	createEmptyWorksetFile,
	loadAuthorityBundle,
	loadCatalog,
	loadWorkset,
	readJsonFile,
	saveAuthorityBundle,
	saveCatalog,
	savePendingReport,
	saveWorkset,
	writeTextFile,
} from '../../core/store.mts';
import type { ReconciliationReportFile } from '../../core/model.mts';

export function loadWebviewsCatalog(context: WebviewsDomainContext) {
	return loadCatalog(context);
}

export function saveWebviewsCatalog(
	context: WebviewsDomainContext,
	catalog: ReturnType<typeof createEmptyWebviewsCatalogFile>,
): void {
	saveCatalog(context, catalog);
}

export function loadWebviewsReconciliationReport(context: WebviewsDomainContext): ReconciliationReportFile {
	return readJsonFile<ReconciliationReportFile>(context.reconciliationReportFile);
}

export function saveWebviewsReconciliationReport(
	context: WebviewsDomainContext,
	report: ReconciliationReportFile,
): void {
	writeTextFile(context.reconciliationReportFile, `${JSON.stringify(report, undefined, '\t')}\n`);
}

export function loadWebviewsWorkset(context: WebviewsDomainContext) {
	return loadWorkset(context);
}

export function saveWebviewsWorkset(
	context: WebviewsDomainContext,
	workset: ReturnType<typeof createEmptyWebviewsWorksetFile>,
): void {
	saveWorkset(context, workset);
}

export function loadSourceTargetContents(context: WebviewsDomainContext, relativeFile: string): string | undefined {
	try {
		return fs.readFileSync(path.join(context.rootDir, ...relativeFile.split('/')), 'utf8');
	} catch {
		return undefined;
	}
}

export function saveLocalizedHtmlSource(context: WebviewsDomainContext, relativeFile: string, contents: string): void {
	writeTextFile(path.join(context.localizedDynamicSourceDir, ...relativeFile.split('/')), contents);
}

export function loadLocalizedHtmlSource(context: WebviewsDomainContext, relativeFile: string): string | undefined {
	try {
		return fs.readFileSync(path.join(context.localizedDynamicSourceDir, ...relativeFile.split('/')), 'utf8');
	} catch {
		return undefined;
	}
}

export function saveLocalizedDynamicSource(
	context: WebviewsDomainContext,
	relativeFile: string,
	contents: string,
): void {
	writeTextFile(path.join(context.localizedDynamicSourceDir, ...relativeFile.split('/')), contents);
}

export function loadLocalizedDynamicSource(context: WebviewsDomainContext, relativeFile: string): string | undefined {
	try {
		return fs.readFileSync(path.join(context.localizedDynamicSourceDir, ...relativeFile.split('/')), 'utf8');
	} catch {
		return undefined;
	}
}

export function deleteLocalizedDynamicSource(context: WebviewsDomainContext, relativeFile: string): void {
	const filePath = path.join(context.localizedDynamicSourceDir, ...relativeFile.split('/'));
	try {
		fs.rmSync(filePath, { force: true });
	} catch {}
}

export { loadAuthorityBundle, saveAuthorityBundle, savePendingReport };

export function createEmptyWebviewsCatalogFile() {
	return createEmptyCatalogFile({
		schemaPath: '../schemas/sourceCatalog.schema.json',
		domain: 'webviews',
		deferredDomains: ['quickpicks', 'formatter'],
	});
}

export function createEmptyWebviewsReconciliationReportFile(): ReconciliationReportFile {
	return createEmptyReconciliationReportFile({
		schemaPath: '../schemas/reconciliationReport.schema.json',
		domain: 'webviews',
	});
}

export function createEmptyWebviewsWorksetFile() {
	return createEmptyWorksetFile({
		schemaPath: '../schemas/translationWorkset.schema.json',
		domain: 'webviews',
		locale: 'zh-cn',
	});
}
