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

  let value = $state('');
  let disabled = $state(false);

  // Entering and removing text from the input area leaves a stray line break behind. This effect
  // looks for that scenario and removes it, restoring the "empty" state (and bringing the
  // placeholder back again in the process).
  $effect(() => {
    const trimmed = value.trim();
    if (trimmed !== value && trimmed === '') {
      value = trimmed;
    }
  });

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

  function onkeydown(event) {
    if (disabled) {
      event.preventDefault();
      return;
    }

    let modifier = event.shiftKey || event.altKey || event.ctrlKey || event.metaKey;
    if (!modifier && event.key === 'Enter') {
      event.preventDefault();
      event.target.closest('form')?.requestSubmit();
    }
  }

  function onpaste(event) {
    if (disabled) {
      event.preventDefault();
    }
  }
</script>

<form {onsubmit}>
  <textarea bind:value hidden {disabled}></textarea>
  <div
    role="textbox"
    tabindex="0"
    contenteditable="plaintext-only"
    class={{
      textarea: true,
      disabled
    }}
    bind:innerText={value}
    {onkeydown}
    {onpaste}
    data-placeholder="Say something..."
  ></div>
  <button type="submit">&raquo;</button>
</form>
