<div class="demo-caption">A project's paths, in a std/ui window</div>
<div class="demo-path">
  <div class="demo-path-bar">
    <ray-switch id="demo-path-simulate" label="Simulate a std/ui window" description="Installs a stand-in for the raylang host, so Browse… shows here."></ray-switch>
  </div>
  <form class="demo-path-form" id="demo-path-form">
    <ray-path-input name="workspace" label="Workspace" mode="folder" value="/Users/ada/Projects/ray-elements" required></ray-path-input>
    <ray-path-input name="config" label="Tokens file" accept=".toml" value="/Users/ada/Projects/ray-elements/tokens.toml" description="The palette, spacing and radii the build reads."></ray-path-input>
    <ray-path-input name="export" label="Export the theme to" mode="save" accept=".css,.json" value="/Users/ada/Desktop/theme.css"></ray-path-input>
    <div class="demo-row"><ray-button type="submit">Save settings</ray-button><span class="demo-muted" id="demo-path-out">In a browser these are plain text fields.</span></div>
  </form>
</div>

<div class="demo-caption">Sizes and states</div>
<div class="demo-grid">
  <ray-path-input label="Small (default)" placeholder="/path/to/file"></ray-path-input>
  <ray-path-input label="Large, filled" size="lg" variant="filled" mode="folder" placeholder="Choose a folder"></ray-path-input>
  <ray-path-input label="Error" value="/Users/ada/missing.toml" error="The file does not exist."></ray-path-input>
  <ray-path-input label="Disabled" value="/etc/hosts" disabled></ray-path-input>
</div>

<script type="module">
  import { getHost, setHost } from "@raylang/elements";

  const baseName = (p) => p.split(/[\\/]/).pop();
  const dirName = (p) => p.slice(0, Math.max(0, p.lastIndexOf("/")));

  // A stand-in for core/raylang.js: in a std/ui window the program answers
  // with the system dialog; here a choice is made up from what was asked.
  const stand = {
    name: "simulated",
    can: (op) => op === "pick_path",
    async pickPath({ mode, directory, suggested }) {
      const dir = directory || "/Users/ada/Projects";
      if (mode === "folder") return `${dirName(dir) || dir}/ray-apps`;
      if (mode === "save") return `/Users/ada/Documents/${suggested || "untitled"}`;
      return `${dir}/${baseName(dir) === "ray-elements" ? "" : "ray-elements/"}tokens.dark.toml`;
    },
  };
  const toggle = document.getElementById("demo-path-simulate");
  toggle.addEventListener("change", () => {
    if (toggle.checked && !getHost()) setHost(stand);
    else if (!toggle.checked && getHost() === stand) setHost(null);
  });

  const form = document.getElementById("demo-path-form");
  const out = document.getElementById("demo-path-out");
  form.addEventListener("change", (e) => {
    if (e.target.localName === "ray-path-input") out.textContent = `${e.target.label}: ${e.target.value}`;
  });
  form.addEventListener("submit", (e) => {
    e.preventDefault();
    out.textContent = `Saved: ${[...new FormData(form).values()].map(baseName).join(", ")}`;
  });
</script>