import { a7 as head, a4 as escape_html, a6 as attr, ab as ssr_context } from '../../../chunks/index.js-BVnekaYQ.js';
import { g as goto } from '../../../chunks/client.js-CmSsxc12.js';
import { p as parseScan } from '../../../chunks/qr.js-BYghGLdS.js';
import '../../../chunks/utils.js-mIi_c4Oz.js';
import '../../../chunks/utils2.js-BQzn9ikS.js';
import '../../../chunks/exports.js-8HOoaa4e.js';
import '../../../chunks/index3.js-C9fZseUW.js';
import '../../../chunks/root.js-l4cSj5gU.js';
import '../../../chunks/false.js-B2gHlHjM.js';
import '../../../chunks/state.svelte.js-DC2zSH9Y.js';

function onDestroy(fn) {
  /** @type {SSRContext} */
  ssr_context.r.on_destroy(fn);
}
function _page($$renderer, $$props) {
  $$renderer.component(($$renderer2) => {
    let { data } = $$props;
    let manual = "";
    let cameraOn = false;
    let controls = null;
    function resolveCode(raw) {
      const parsed = parseScan(raw);
      const code = parsed?.code ?? raw.trim();
      if (code) goto(`/scan?code=${encodeURIComponent(code)}`, {});
    }
    function stopCamera() {
      controls?.stop();
      controls = null;
      cameraOn = false;
    }
    let usbBuffer = "";
    let usbTimer = null;
    function onKeydown(e) {
      const target = e.target;
      if (target && (target.tagName === "INPUT" || target.tagName === "TEXTAREA")) return;
      if (e.key === "Enter") {
        if (usbBuffer.length >= 3) resolveCode(usbBuffer);
        usbBuffer = "";
        return;
      }
      if (e.key.length === 1) {
        usbBuffer += e.key;
        if (usbTimer) clearTimeout(usbTimer);
        usbTimer = setTimeout(() => usbBuffer = "", 300);
      }
    }
    onDestroy(() => {
      if (typeof window !== "undefined") window.removeEventListener("keydown", onKeydown);
      stopCamera();
    });
    head("19hdok6", $$renderer2, ($$renderer3) => {
      $$renderer3.title(($$renderer4) => {
        $$renderer4.push(`<title>Escanear · Inventario EDUmind</title>`);
      });
    });
    $$renderer2.push(`<div class="mx-auto max-w-xl"><div class="mb-6"><h1 class="text-2xl font-bold text-slate-900">Escanear equipo</h1> <p class="text-slate-600">Escanea el QR con la cámara o el lector USB, o escribe el código del equipo.</p></div> `);
    if (data.notFoundCode) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<p class="mb-4 rounded-lg bg-amber-50 px-4 py-3 text-sm font-medium text-amber-800" role="alert">No se encontró ningún equipo con el código <strong class="font-mono">${escape_html(data.notFoundCode)}</strong>.</p>`);
    } else {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--> <section class="card p-6"><form class="flex gap-2"><input class="input font-mono"${attr("value", manual)} placeholder="Código del equipo (ej. POR-0001)" aria-label="Código del equipo" autofocus=""/> <button class="btn-primary shrink-0" type="submit">Buscar</button></form> <p class="mt-2 text-xs text-slate-500">Un lector USB también funciona aquí: apunta y dispara, se rellena solo.</p></section> <section class="card mt-4 p-6"><div class="flex items-center justify-between"><h2 class="font-semibold text-slate-900">Cámara</h2> `);
    if (cameraOn) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<button class="btn-secondary">Detener</button>`);
    } else {
      $$renderer2.push("<!--[-1-->");
      $$renderer2.push(`<button class="btn-primary">Activar cámara</button>`);
    }
    $$renderer2.push(`<!--]--></div> `);
    {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--> `);
    if (cameraOn) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<div class="mt-4 overflow-hidden rounded-xl bg-black"><video class="aspect-video w-full object-cover" autoplay="" playsinline=""></video></div> <p class="mt-2 text-center text-sm text-slate-500">Apunta al código QR de la etiqueta…</p>`);
    } else {
      $$renderer2.push("<!--[-1-->");
      $$renderer2.push(`<div class="mt-4 flex aspect-video items-center justify-center rounded-xl border-2 border-dashed border-slate-200 text-slate-400"><span>La cámara aparecerá aquí</span></div>`);
    }
    $$renderer2.push(`<!--]--></section></div>`);
  });
}

export { _page as default };
//# sourceMappingURL=_page.svelte.js-DuSv0aJO.js.map
