// $lib/services/messages.ts

import { queryMessages } from '$lib/data/queryMessages';

// The createMessage function now accepts a messageData parameter.
export function createMessage(messageData) {
  const currentDate = new Date().toLocaleString();
  const newMessageObj = {
    text: messageData.text,
    timestamp: currentDate,
    sender: messageData.sender,
    type: messageData.type,
    composite: messageData.composite || null
  };

  // We use the $ prefix to get the value out of a Svelte store
  // and then set the new value back into the store.
  queryMessages.update(oldMessages => [...oldMessages, newMessageObj]);
}

export function clearMessages() {
  queryMessages.set([]);
}
