const std = @import("std");
const ziex = @import("ziex");

const build_zon = @import("build.zig.zon");

pub fn build(b: *std.Build) !void {
    // --- Target and Optimize from `zig build` arguments ---
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // --- Deps --- //
    const playground_dep = b.dependency("playground", .{});
    const ziex_dep = b.dependency("ziex", .{ .optimize = optimize, .target = target });
    const tree_sitter_dep = ziex_dep.builder.dependency("tree_sitter", .{ .optimize = optimize, .target = target });
    const tree_sitter_zx_dep = ziex_dep.builder.dependency("tree_sitter_zx", .{ .optimize = optimize, .target = target, .@"build-shared" = false });
    // const tree_sitter_mdzx_dep = ziex_dep.builder.dependency("tree_sitter_mdzx", .{ .optimize = optimize, .target = target, .@"build-shared" = false });

    // --- Assets -- //
    const pg_assets = playground_dep.namedWriteFiles("playground_assets");
    const install_pg = b.addInstallDirectory(.{
        .source_dir = pg_assets.getDirectory(),
        .install_dir = .prefix,
        .install_subdir = "static/assets/playground",
    });

    // -- Steps: pg - installs playground assets --- //
    const pg_step = b.step("pg", "Install playground assets");
    pg_step.dependOn(&install_pg.step);

    // --- ZX App Executable --- //
    const app_exe = b.addExecutable(.{
        .name = "ziex_dev",
        .root_module = b.createModule(.{
            .root_source_file = b.path("app/main.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });
    app_exe.root_module.addImport("tree_sitter", tree_sitter_dep.module("tree_sitter"));
    app_exe.root_module.addImport("tree_sitter_zx", tree_sitter_zx_dep.module("tree_sitter_zx"));
    app_exe.step.dependOn(&install_pg.step);

    // --- ZX setup: wires dependencies and adds `zx`/`dev` build steps --- //
    var ziex_b = try ziex.init(b, app_exe, .{
        .app = .{
            .path = b.path("app"),
            .copy_embedded_sources = true,
        },
        .client = .{ .jsglue_href = "/assets/_/main.js" },
        .cli = .{ .optimize = optimize },
    });

    var assetsdir = ziex_b.assetsdir;
    const tailwindcss_b = tailwindcss.addBuild(b, .{
        .config = .{
            .input = b.path("app/assets/docs.css"),
            // .output = assetsdir.path(b, "docs.css"),
            .minify = true,
            .optimize = true,
            .map = false,
        },
    });
    _ = b.addInstallFile(tailwindcss_b.file, "static/assets/docs.css");

    ziex_b.plugin(ziex.plugins.esbuild(b, .{
        .input = b.path("app/scripts/react.ts"),
        .output = assetsdir.path(b, "react.js"),
        .optimize = optimize,
    }));
    ziex_b.plugin(ziex.plugins.esbuild(b, .{
        .input = b.path("app/scripts/client.ts"),
        .output = assetsdir.path(b, "_/main.js"),
        .optimize = optimize,
    }));
    ziex_b.plugin(ziex.plugins.esbuild(b, .{
        .input = b.path("app/scripts/docs.ts"),
        .output = assetsdir.path(b, "docs.js"),
        .optimize = optimize,
    }));

    // TODO: Fix issue with outfile
    // bunjs.addBuildRun(b, .{ .config = .{
    //     .entrypoints = &.{b.path("app/scripts/docs.ts")},
    //     .outfile = assetsdir.path(b, "docs.js"),
    // } });
    const bi = bunjs.addBuild(b, .{
        .name = "playground_scripts",
        .config = .{
            .entrypoints = &.{
                b.path("app/pages/playground/scripts/editor.ts"),
                b.path("app/pages/playground/scripts/workers/runner.ts"),
                b.path("app/pages/playground/scripts/workers/zig.ts"),
                b.path("app/pages/playground/scripts/workers/zx.ts"),
                b.path("app/pages/playground/scripts/workers/zls.ts"),
            },
            .define = &.{
                .{
                    .key = "VERSION",
                    .value = b.fmt("\"{s}\"", .{build_zon.version}),
                },
            },
            // .outdir = assetsdir.path(b, "playground/"),
        },
    });

    b.installDirectory(.{
        .source_dir = bi.dir,
        .install_dir = .prefix,
        .install_subdir = "static/assets/playground",
    });

    b.installDirectory(.{
        .source_dir = ziex_b.ziex_js.dep.path("."),
        .install_dir = .prefix,
        .include_extensions = &.{ ".js", ".ts" },
        .install_subdir = "pkg/ziex",
    });

    // ziex_b.installZiexJs(.{});
}

const bunjs = @import("bunjs");
const tailwindcss = @import("tailwindcss");
