using JSON
using NodeJS_22_jll
using Documenter

# Load the real search index from test examples (already built!)
function load_real_search_index(search_index_path)
    # Use the search index from the provided path
    # search_index_path = joinpath(@__DIR__, "../../docs/build/search_index.js")

    if !isfile(search_index_path)
        error("Search index not found at: $search_index_path")
    end

    # Read and parse the JavaScript file
    content = read(search_index_path, String)

    # Find the JSON data after "var documenterSearchIndex = "
    json_start = findfirst("var documenterSearchIndex = ", content)
    if json_start === nothing
        error("Invalid search index format: missing variable declaration")
    end

    # Extract JSON content (everything after the variable declaration)
    json_content = content[(last(json_start) + 1):end]

    # Parse the JSON
    parsed = JSON.parse(json_content)
    return parsed["docs"]  # Return just the docs array
end

# Simple function that uses the existing search.js with real search data
function real_search(query::String, search_index_path::String)
    search_index_data = load_real_search_index(search_index_path)

    # Read the JS wrapper and inject data
    wrapper_js = read(joinpath(@__DIR__, "wrapper.js"), String)
    wrapper_js = replace(wrapper_js, "__SEARCH_INDEX__" => JSON.json(search_index_data))
    wrapper_js = replace(wrapper_js, "__QUERY__" => "\"" * query * "\"")


    # Write the wrapper to a temporary file and run it
    return mktemp(@__DIR__) do path, io
        write(io, wrapper_js)
        close(io)
        cd(@__DIR__) do
            # Install minisearch if it's not there
            if !isdir("node_modules") || !isfile("node_modules/minisearch/package.json")
                version = Documenter.HTMLWriter.MINISEARCH_VERSION
                if version === nothing
                    error("Could not find minisearch version in search.js")
                end
                # We have to pass --prefix here, otherwise npm might try to install
                # minisearch in a different location depending on the environment.
                run(`$(NodeJS_22_jll.npm) --prefix . install minisearch@$(version)`)
            end
            result = read(`$(NodeJS_22_jll.node) $path`, String)
            return JSON.parse(strip(result))
        end
    end
end
