import { sql } from "kysely";
export function selectWorkingDiff(args) {
    const db = args.lix.db;
    // Active version details
    const workingCommitIdQ = db
        .selectFrom("active_version")
        .innerJoin("version", "active_version.version_id", "version.id")
        .select((eb) => eb.ref("version.working_commit_id").as("id"));
    // Optimize: previous checkpoint is the parent of the current working commit
    const latestCheckpointCommitIdQ = db
        .selectFrom("commit_edge as ce")
        .innerJoin("commit as p", "p.id", "ce.parent_id")
        .innerJoin("entity_label as el", (join) => join
        .onRef("el.entity_id", "=", "p.id")
        .on("el.schema_key", "=", "lix_commit"))
        .innerJoin("label as l", "l.id", "el.label_id")
        .whereRef("ce.child_id", "=", workingCommitIdQ)
        .where("l.name", "=", "checkpoint")
        .select("p.id")
        .limit(1);
    // Base SELECT: rows from the working change set
    const base = db
        .selectFrom("change as ch")
        .innerJoin("change_set_element as cse", "cse.change_id", "ch.id")
        .where("cse.change_set_id", "=", sql `(select change_set_id from wcs)`)
        .where("ch.file_id", "!=", "lix_sdk")
        .leftJoin("change_set_element as bcse", (join) => join
        .onRef("bcse.entity_id", "=", "ch.entity_id")
        .onRef("bcse.schema_key", "=", "ch.schema_key")
        .onRef("bcse.file_id", "=", "ch.file_id")
        .on("bcse.change_set_id", "=", sql `(select change_set_id from ccs)`))
        // no join to `change bc` here — project ids only
        .select((eb) => [
        eb.ref("ch.entity_id").as("entity_id"),
        eb.ref("ch.schema_key").as("schema_key"),
        eb.ref("ch.file_id").as("file_id"),
        sql `NULL`.as("before_version_id"),
        eb.ref("bcse.change_id").as("before_change_id"),
        sql `(select id from cc)`.as("before_commit_id"),
        sql `NULL`.as("after_version_id"),
        eb.ref("ch.id").as("after_change_id"),
        sql `(select id from wc)`.as("after_commit_id"),
        sql `CASE
        WHEN bcse.change_id IS NOT NULL AND json(ch.snapshot_content) IS NULL THEN 'removed'
        WHEN bcse.change_id IS NULL AND json(ch.snapshot_content) IS NOT NULL THEN 'added'
        WHEN bcse.change_id IS NOT NULL AND json(ch.snapshot_content) IS NOT NULL AND bcse.change_id != ch.id THEN 'modified'
        ELSE 'unchanged'
      END`.as("status"),
    ]);
    // Unchanged rows = non-inherited, tracked state rows without a working CSE entry
    const unchanged = db
        .selectFrom("state_by_version as sa")
        .whereRef("sa.version_id", "=", db.selectFrom("active_version").select("active_version.version_id"))
        .where("sa.inherited_from_version_id", "is", null)
        .where("sa.untracked", "=", false)
        // Anti-join to ensure no working edit exists for this triple
        .leftJoin("change_set_element as wcse", (join) => join
        .onRef("wcse.entity_id", "=", "sa.entity_id")
        .onRef("wcse.schema_key", "=", "sa.schema_key")
        .onRef("wcse.file_id", "=", "sa.file_id")
        .on("wcse.change_set_id", "=", sql `(select change_set_id from wcs)`))
        .where("wcse.change_id", "is", null)
        // For unchanged rows, before == after; reuse sa.* and avoid checkpoint joins
        .select((eb) => [
        eb.ref("sa.entity_id").as("entity_id"),
        eb.ref("sa.schema_key").as("schema_key"),
        eb.ref("sa.file_id").as("file_id"),
        sql `NULL`.as("before_version_id"),
        eb.ref("sa.change_id").as("before_change_id"),
        sql `(select id from cc)`.as("before_commit_id"),
        sql `NULL`.as("after_version_id"),
        eb.ref("sa.change_id").as("after_change_id"),
        sql `(select id from wc)`.as("after_commit_id"),
        sql `'unchanged'`.as("status"),
    ]);
    // CTEs for constants used across both UNION arms
    const union = sql `(
        select * from (${base}) as w
        union all
        select * from (${unchanged}) as u
    )`.as("diff");
    return db
        .with("wc", () => workingCommitIdQ)
        .with("cc", () => latestCheckpointCommitIdQ)
        .with("wcs", () => db
        .selectFrom("commit")
        .where("id", "=", sql `(select id from wc)`)
        .select(["change_set_id"]))
        .with("ccs", () => db
        .selectFrom("commit")
        .where("id", "=", sql `(select id from cc)`)
        .select(["change_set_id"]))
        .selectFrom(union);
}
//# sourceMappingURL=select-working-diff.js.map