"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReactRouterVersionBucket = exports.ReactRouterMode = void 0;
exports.getReactRouterMode = getReactRouterMode;
exports.getReactRouterModeName = getReactRouterModeName;
const semver_1 = require("semver");
const fast_glob_1 = __importDefault(require("fast-glob"));
const package_json_light_1 = require("../../utils/package-json-light");
const package_json_1 = require("../../utils/package-json");
const semver_2 = require("../../utils/semver");
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const semver = __importStar(require("semver"));
var ReactRouterMode;
(function (ReactRouterMode) {
    ReactRouterMode["V6"] = "v6";
    ReactRouterMode["V7_FRAMEWORK"] = "v7-framework";
    ReactRouterMode["V7_DATA"] = "v7-data";
    ReactRouterMode["V7_DECLARATIVE"] = "v7-declarative";
})(ReactRouterMode || (exports.ReactRouterMode = ReactRouterMode = {}));
const IGNORE_PATTERNS = [
    '**/node_modules/**',
    '**/dist/**',
    '**/build/**',
    '**/public/**',
    '**/.next/**',
];
exports.getReactRouterVersionBucket = (0, semver_2.createVersionBucket)();
async function hasReactRouterConfig({ installDir, }) {
    const configMatches = await (0, fast_glob_1.default)('**/react-router.config.@(ts|js|tsx|jsx)', {
        dot: true,
        cwd: installDir,
        ignore: IGNORE_PATTERNS,
    });
    return configMatches.length > 0;
}
async function hasCreateBrowserRouter({ installDir, }) {
    const sourceFiles = await (0, fast_glob_1.default)('**/*.@(ts|tsx|js|jsx)', {
        dot: true,
        cwd: installDir,
        ignore: IGNORE_PATTERNS,
    });
    for (const file of sourceFiles) {
        try {
            const filePath = path.join(installDir, file);
            const content = fs.readFileSync(filePath, 'utf-8');
            if (content.includes('createBrowserRouter')) {
                return true;
            }
        }
        catch {
            continue;
        }
    }
    return false;
}
async function hasDeclarativeRouter({ installDir, }) {
    const sourceFiles = await (0, fast_glob_1.default)('**/*.@(ts|tsx|js|jsx)', {
        dot: true,
        cwd: installDir,
        ignore: IGNORE_PATTERNS,
    });
    for (const file of sourceFiles) {
        try {
            const filePath = path.join(installDir, file);
            const content = fs.readFileSync(filePath, 'utf-8');
            if (content.includes('<BrowserRouter') ||
                (content.includes('BrowserRouter') &&
                    (content.includes('from "react-router-dom"') ||
                        content.includes("from 'react-router-dom'")))) {
                return true;
            }
        }
        catch {
            continue;
        }
    }
    return false;
}
/**
 * Detect React Router mode. Pure — returns null if ambiguous.
 */
async function getReactRouterMode(options) {
    const { installDir } = options;
    const packageJson = await (0, package_json_light_1.tryGetPackageJson)(options);
    if (!packageJson)
        return null;
    const reactRouterVersion = (0, package_json_1.getPackageVersion)('react-router-dom', packageJson) ||
        (0, package_json_1.getPackageVersion)('react-router', packageJson);
    if (!reactRouterVersion) {
        return null;
    }
    const coercedVersion = semver.coerce(reactRouterVersion);
    const majorVersion = coercedVersion ? (0, semver_1.major)(coercedVersion) : null;
    if (majorVersion === 6) {
        return ReactRouterMode.V6;
    }
    if (majorVersion === 7) {
        const hasConfig = await hasReactRouterConfig({ installDir });
        if (hasConfig)
            return ReactRouterMode.V7_FRAMEWORK;
        const hasDataMode = await hasCreateBrowserRouter({ installDir });
        if (hasDataMode)
            return ReactRouterMode.V7_DATA;
        const hasDeclarative = await hasDeclarativeRouter({ installDir });
        if (hasDeclarative)
            return ReactRouterMode.V7_DECLARATIVE;
        // v7 but can't detect mode
        return null;
    }
    return null;
}
function getReactRouterModeName(mode) {
    switch (mode) {
        case ReactRouterMode.V6:
            return 'v6';
        case ReactRouterMode.V7_FRAMEWORK:
            return 'v7 Framework mode';
        case ReactRouterMode.V7_DATA:
            return 'v7 Data mode';
        case ReactRouterMode.V7_DECLARATIVE:
            return 'v7 Declarative mode';
    }
}
