@echo off
setlocal

title le vencord installer with le language plugin

where git >nul 2>nul
if %errorlevel% neq 0 (
    echo [ERROR] Git is not installed or is not in PATH.
    echo Install Git from https://git-scm.com/downloads
    pause
    exit /b 1
)

where node >nul 2>nul
if %errorlevel% neq 0 (
    echo [ERROR] Node.js is not installed or is not in PATH.
    echo Install Node.js LTS from https://nodejs.org/
    pause
    exit /b 1
)

if not exist "package.json" (
    echo [ERROR] This script must be run inside your Vencord source folder.
    echo Put this .bat file in the Vencord folder, then run it again.
    pause
    exit /b 1
)

if not exist "src" (
    echo [ERROR] This script must be run inside your Vencord source folder.
    echo Put this .bat file in the Vencord folder, then run it again.
    pause
    exit /b 1
)

echo [OK] Git and Node.js found.
echo.

echo Setting up pnpm...
call corepack enable >nul 2>nul
call corepack prepare pnpm@latest --activate >nul 2>nul

where pnpm >nul 2>nul
if %errorlevel% neq 0 (
    echo Corepack did not make pnpm available. Trying npm install instead...
    call npm install -g pnpm
    if %errorlevel% neq 0 (
        echo [ERROR] Failed to install pnpm.
        echo Try running this installer as Administrator.
        pause
        exit /b 1
    )
)

echo.
echo Installing dependencies...
call pnpm install
if %errorlevel% neq 0 (
    echo [ERROR] pnpm install failed.
    pause
    exit /b 1
)

echo.
echo Creating 1Languagent plugin...

if not exist "src\plugins" mkdir "src\plugins"
if not exist "src\plugins\1Languagent" mkdir "src\plugins\1Languagent"

> "src\plugins\1Languagent\index.ts" (
    echo import definePlugin from "@utils/types";
    echo import "./style.css";
    echo.
    echo const server_id = "1213783198279012372";
    echo const channel_id = "1508079357388918860";
    echo const lang_class = "languagent";
    echo.
    echo function updateClass^(^) {
    echo     const [, channels, guildId, channelId] = window.location.pathname.split^("/"^);
    echo.
    echo     const isTarget =
    echo         channels === "channels" ^&^&
    echo         guildId === server_id ^&^&
    echo         channelId === channel_id;
    echo.
    echo     document.body.classList.toggle^(lang_class, isTarget^);
    echo }
    echo.
    echo let originalPushState: typeof history.pushState;
    echo let originalReplaceState: typeof history.replaceState;
    echo.
    echo export default definePlugin^({
    echo     name: "Languagen't Season 2",
    echo     description: "This is the Languagen't S2 font plugin for the in-language-chat channel.",
    echo     authors: [{ name: "kiverix", id: 504305306625638400n }],
    echo.
    echo     start^(^) {
    echo         updateClass^(^);
    echo.
    echo         originalPushState = history.pushState;
    echo         originalReplaceState = history.replaceState;
    echo.
    echo         history.pushState = function ^(...args^) {
    echo             const ret = originalPushState.apply^(this, args^);
    echo             queueMicrotask^(updateClass^);
    echo             return ret;
    echo         };
    echo.
    echo         history.replaceState = function ^(...args^) {
    echo             const ret = originalReplaceState.apply^(this, args^);
    echo             queueMicrotask^(updateClass^);
    echo             return ret;
    echo         };
    echo.
    echo         window.addEventListener^("popstate", updateClass^);
    echo     },
    echo.
    echo     stop^(^) {
    echo         document.body.classList.remove^(lang_class^);
    echo.
    echo         if ^(originalPushState^) history.pushState = originalPushState;
    echo         if ^(originalReplaceState^) history.replaceState = originalReplaceState;
    echo.
    echo         window.removeEventListener^("popstate", updateClass^);
    echo     }
    echo }^);
)

> "src\plugins\1Languagent\style.css" (
    echo @font-face {
    echo     font-family: "Languagen't";
    echo     src: url^("https://kiverix.net/font/languagent.ttf"^) format^("truetype"^);
    echo     font-weight: normal;
    echo     font-style: normal;
    echo }
    echo.
    echo body.languagent [class*="messageContent"],
    echo body.languagent [class*="username"],
    echo body.languagent [class*="markup"],
    echo body.languagent [class*="contents"] [class*="markup"] {
    echo     font-family: "Languagen't", sans-serif !important;
    echo }
)

echo.
echo Building Vencord...
call pnpm build
if %errorlevel% neq 0 (
    echo [ERROR] Build failed.
    pause
    exit /b 1
)

echo.
echo Injecting Vencord into Discord...
echo.
echo LOOK AT ME! PRESS ENTER IF YOUR DISCORD DOES USE THE DEFAULT INSTALL LOCATION!!! ELSE DOWN ARROW THEN ENTER!!!
echo LOOK AT ME! PRESS ENTER IF YOUR DISCORD DOES USE THE DEFAULT INSTALL LOCATION!!! ELSE DOWN ARROW THEN ENTER!!!
echo LOOK AT ME! PRESS ENTER IF YOUR DISCORD DOES USE THE DEFAULT INSTALL LOCATION!!! ELSE DOWN ARROW THEN ENTER!!!
echo.
call pnpm inject
if %errorlevel% neq 0 (
    echo [ERROR] Inject failed.
    echo Make sure Discord is installed and fully closed, then try again.
    pause
    exit /b 1
)

echo.
echo Done!
echo.
echo If Discord was open, it should be closed by now.
echo Simply open it again and enable the plugin in settings^>plugins then search for Languagen't Season 2.

pause
endlocal