import { actions } from 'astro:actions';
import { actionErrorPopup } from '@util/action.ts';

export async function updateSettings(changes: { [k: string]: string | null }) {
  const { error } = await actions.settings.setSettings({
    settings: Object.entries(changes).reduce(
      (prev, curr) => {
        prev.push({ name: curr[0], value: curr[1] });
        return prev;
      },
      [] as { name: string; value: string | null }[]
    )
  });
  if (error) {
    actionErrorPopup(error);
    return false;
  }
  return true;
}
