'use server';
import { revalidatePath } from 'next/cache';
import { resolve } from '../../lib/server-container.js';
import { updateSettings as updateSettingsSingleton } from '../../../../../packages/core/src/infrastructure/services/settings.service.js';
function deepMerge(target, source) {
    const result = { ...target };
    for (const key of Object.keys(source)) {
        const sourceVal = source[key];
        if (sourceVal === undefined)
            continue;
        const targetVal = target[key];
        if (targetVal !== null &&
            targetVal !== undefined &&
            typeof targetVal === 'object' &&
            !Array.isArray(targetVal) &&
            !(targetVal instanceof Date) &&
            sourceVal !== null &&
            typeof sourceVal === 'object' &&
            !Array.isArray(sourceVal) &&
            !(sourceVal instanceof Date)) {
            result[key] = deepMerge(targetVal, sourceVal);
        }
        else {
            result[key] = sourceVal;
        }
    }
    return result;
}
export async function updateSettingsAction(partial) {
    try {
        const loadUseCase = resolve('LoadSettingsUseCase');
        const current = await loadUseCase.execute();
        const merged = deepMerge(current, partial);
        merged.updatedAt = new Date();
        const updateUseCase = resolve('UpdateSettingsUseCase');
        await updateUseCase.execute(merged);
        updateSettingsSingleton(merged);
        revalidatePath('/', 'layout');
        return { success: true };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to update settings';
        return { success: false, error: message };
    }
}
