---
import DefaultLayout from './DefaultLayout.astro'

const { frontmatter } = Astro.props
---

<DefaultLayout title={frontmatter.title}>
  <div class="container">
    <div class="space-content my-12">
      <slot />
    </div>
  </div>
</DefaultLayout>

<!-- Copy Code Buttons based on: https://timneubauer.dev/blog/copy-code-button-in-astro/ -->
<script>
  initCopyCodeButtons();
  document.addEventListener('astro:after-swap', initCopyCodeButtons);

  function initCopyCodeButtons() {
    console.log("Initializing Copy Code Buttons...");
    let copyButtonLabel = "Copy Code";
    let codeBlocks = Array.from(document.querySelectorAll("pre"));

    for (let codeBlock of codeBlocks) {
      let wrapper = document.createElement("div");
      wrapper.style.position = "relative";

      let copyButton = document.createElement("button");
      copyButton.className = "copy-code";
      copyButton.innerHTML = copyButtonLabel;

      codeBlock.setAttribute("tabindex", "0");
      codeBlock.appendChild(copyButton);
      // wrap codebock with relative parent element
      if (codeBlock.parentNode) codeBlock.parentNode.insertBefore(wrapper, codeBlock);
      wrapper.appendChild(codeBlock);

      copyButton.addEventListener("click", async () => {
        await copyCode(codeBlock, copyButton);
      });
    }

    async function copyCode(block, button) {
      let code = block.querySelector("code");
      let text = code.innerText;

      await navigator.clipboard.writeText(text);

      // visual feedback that task is completed
      button.innerText = "Code Copied!";

      setTimeout(() => {
        button.innerText = copyButtonLabel;
      }, 700);
    }
  }
</script>

<style is:global>
  .copy-code {
    position: absolute;
    top: 0;
    right: 0;
    background-color: var(--primary-300);
    color: var(--neutral-100);
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    line-height: 1rem;
    border: none;
    border-top-right-radius: 0.35rem;
    border-bottom-left-radius: 0.35rem;
    box-shadow: -1px 1px 8px var(--primary-300);
  }
  .copy-code:is(.darkmode *) {
    background-color: var(--secondary-100);
    color: var(--dark-100);
    box-shadow: -1px 1px 8px var(--secondary-100);
  }

  .copy-code:hover {
    background-color: var(--primary-500);
    box-shadow: -1px 1px 8px var(--primary-500);
  }
  .copy-code:hover:is(.darkmode *) {
    background-color: var(--secondary-500);
    box-shadow: -1px 1px 8px var(--secondary-500);
  }
  .notification:is(.darkmode *) a:not(.button) {
    color: var(--secondary-400);
  }
  .notification:is(.darkmode *) a:not(.button):visited {
    color: var(--secondary-500);
  }

</style>