#!/bin/bash
# FastLED JavaScript Linting Script (Node.js + ESLint + JSDoc Type Checking - FAST!)

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}FAST FastLED JavaScript Linting (Node.js + ESLint + JSDoc Type Checking - FAST!)${NC}"

# Check if ESLint is installed
if [ ! -f ".cache/js-tools/node_modules/.bin/eslint.cmd" ]; then
    echo -e "${RED}ERROR: ESLint not found. Run: uv run ci/setup-js-linting-fast.py${NC}"
    exit 1
fi

# Find JavaScript files in WASM platform
JS_FILES=$(find src/platforms/wasm -name "*.js" -type f 2>/dev/null)

if [ -z "$JS_FILES" ]; then
    echo -e "${YELLOW}WARNING: No JavaScript files found in src/platforms/wasm/${NC}"
    exit 0
fi

echo -e "${BLUE}Found JavaScript files:${NC}"
echo "$JS_FILES" | sed 's/^/  /'

# Run ESLint using config from ci/ directory
echo -e "${BLUE}Running ESLint...${NC}"
cd .cache/js-tools
if "./node_modules/.bin/eslint.cmd" --no-eslintrc --no-inline-config -c ../../ci/.eslintrc.js ../../src/platforms/wasm/compiler/*.js ../../src/platforms/wasm/compiler/modules/*.js; then
    echo -e "${GREEN}✅ ESLint completed successfully${NC}"
else
    echo -e "${RED}ERROR: ESLint failed${NC}"
    exit 1
fi

# Run JSDoc Type Checking using TypeScript
echo -e "${BLUE}Running JSDoc Type Checking...${NC}"
cd ../../src/platforms/wasm/compiler

# Check if TypeScript is available (local installation first, then global)
if [ -f "../../../../.cache/js-tools/node_modules/.bin/tsc.cmd" ]; then
    echo -e "${BLUE}Using local TypeScript for JSDoc type checking...${NC}"
    # Run TypeScript and filter out Node.js events library errors
    TSC_OUTPUT=$(../../../../.cache/js-tools/node_modules/.bin/tsc.cmd --noEmit --project jsconfig.json 2>&1)
    TSC_EXIT_CODE=$?
    # Filter out the specific Node.js events error
    FILTERED_OUTPUT=$(echo "$TSC_OUTPUT" | grep -v "node_modules/events/events.js.*Property 'context' does not exist on type 'Error'" || true)

    if [ -n "$FILTERED_OUTPUT" ]; then
        echo "$FILTERED_OUTPUT"
        if [ $TSC_EXIT_CODE -ne 0 ]; then
            echo -e "${RED}❌ JSDoc type checking failed with errors${NC}"
            exit 1
        fi
    fi
    echo -e "${GREEN}✅ JSDoc type checking completed successfully${NC}"
elif [ -f "../../../../.cache/js-tools/node_modules/.bin/tsc" ]; then
    echo -e "${BLUE}Using local TypeScript for JSDoc type checking...${NC}"
    # Run TypeScript and filter out Node.js events library errors
    TSC_OUTPUT=$(../../../../.cache/js-tools/node_modules/.bin/tsc --noEmit --project jsconfig.json 2>&1)
    TSC_EXIT_CODE=$?
    # Filter out the specific Node.js events error
    FILTERED_OUTPUT=$(echo "$TSC_OUTPUT" | grep -v "node_modules/events/events.js.*Property 'context' does not exist on type 'Error'" || true)

    if [ -n "$FILTERED_OUTPUT" ]; then
        echo "$FILTERED_OUTPUT"
        if [ $TSC_EXIT_CODE -ne 0 ]; then
            echo -e "${RED}❌ JSDoc type checking failed with errors${NC}"
            exit 1
        fi
    fi
    echo -e "${GREEN}✅ JSDoc type checking completed successfully${NC}"
elif command -v npx >/dev/null 2>&1 && npx tsc --version >/dev/null 2>&1; then
    echo -e "${BLUE}Using global TypeScript for JSDoc type checking...${NC}"
    # Run TypeScript and filter out Node.js events library errors
    TSC_OUTPUT=$(npx tsc --noEmit --project jsconfig.json 2>&1)
    TSC_EXIT_CODE=$?
    # Filter out the specific Node.js events error
    FILTERED_OUTPUT=$(echo "$TSC_OUTPUT" | grep -v "node_modules/events/events.js.*Property 'context' does not exist on type 'Error'" || true)

    if [ -n "$FILTERED_OUTPUT" ]; then
        echo "$FILTERED_OUTPUT"
        if [ $TSC_EXIT_CODE -ne 0 ]; then
            echo -e "${RED}❌ JSDoc type checking failed with errors${NC}"
            exit 1
        fi
    fi
    echo -e "${GREEN}✅ JSDoc type checking completed successfully${NC}"
else
    echo -e "${YELLOW}⚠️  TypeScript not found, skipping JSDoc type checking${NC}"
    echo -e "${BLUE}💡 To enable JSDoc checking: Run the ./install script or npm install -g typescript${NC}"
fi

echo -e "${GREEN}SUCCESS: JavaScript linting and type checking completed successfully${NC}"