import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

const themeFile = (path) =>
    readFile(new URL(`../../${path}`, import.meta.url), "utf8").catch(() => "");

const [template, controller, languageSwitcher, seo] = await Promise.all([
    themeFile("templates/components/shop/ProductNavigation.html.twig"),
    themeFile("assets/controllers/product_navigation_controller.js"),
    themeFile("templates/partials/_language_switcher.html.twig"),
    themeFile("templates/partials/seo.html.twig"),
]);

assert.match(template, /data-controller="product-navigation"/u);
assert.match(template, /class="[^"]*\bsplide\b/u);
assert.match(
    template,
    /class="[^"]*\bsplide__track\b[^"]*\boverflow-hidden\b[^"]*\bborder-b\b[^"]*\bborder-\[#8A5A34\]\/35\b/u,
);
assert.match(template, /class="[^"]*\bsplide__list\b[^"]*\bpb-4\b/u);
assert.doesNotMatch(template, /class="[^"]*\bsplide__list\b[^"]*\bborder-b\b/u);
assert.match(template, /class="[^"]*\bsplide__slide\b/u);
assert.match(template, /data-product-navigation-target="active"/u);
assert.doesNotMatch(template, /overflow-x-auto/u);
assert.doesNotMatch(template, /preventClick/u);
assert.match(template, /this\.showCategoryMenu\|default\(true\)/u);
assert.match(
    template,
    /showCategoryMenu\s*\?\s*\{\s*slug:\s*product\.category\.slug\s*\}\s*:\s*\{\s*\}/u,
);
for (const localizedTemplate of [languageSwitcher, seo]) {
    assert.match(localizedTemplate, /localized_page_routes\(\)/u);
    assert.doesNotMatch(localizedTemplate, /\[['"]fr['"],\s*['"]en['"]\]/u);
    assert.doesNotMatch(localizedTemplate, /shop_product|categorySlug/u);
    assert.doesNotMatch(localizedTemplate, /\.translate\(/u);
}
assert.match(languageSwitcher, /localizedRoutes\.routes/u);
assert.match(seo, /localizedRoutes\.default\.absoluteUrl/u);

assert.match(controller, /import\s+\{\s*Splide\s*\}\s+from\s+"@splidejs\/splide"/u);
assert.match(controller, /drag:\s*true/u);
assert.match(controller, /dragMinThreshold:\s*\{[\s\S]*mouse:\s*10/u);
assert.match(controller, /focus:\s*0/u);
assert.match(controller, /this\.activeTarget/u);
assert.match(controller, /this\.splide\??\.go/u);
assert.match(controller, /disconnect\(\)/u);
assert.match(controller, /destroy\(true\)/u);

console.log(
    "OK — la navigation produit Heritage utilise Splide, conserve les clics et masque le scroll natif.",
);
