<script>
  let { sendMessage = async (message) => {} } = $props();

  let form = $state(null);
  let value = $state('');
  // This doesn't seem to work with a contenteditable two-way bound svelte
  // thing:
  let disabled = $state(false);

  async function onsubmit(event) {
    event.preventDefault();
    disabled = true;
    try {
      await sendMessage(value);
      value = '';
    } finally {
      disabled = false;
    }
  }

  function onkeydown(event) {
    let modifier = event.shiftKey || event.altKey || event.ctrlKey || event.metaKey;
    if (!modifier && event.key === 'Enter') {
      event.preventDefault();
      form?.requestSubmit?.();
    }
  }
</script>

<form {onsubmit} bind:this={form}>
  <div
    contenteditable="plaintext-only"
    class="textarea"
    bind:textContent={value}
    {onkeydown}
    {disabled}
    placeholder="Say something..."
  ></div>
  <button type="submit">&raquo;</button>
</form>
