/**
 * Creates the public 'state' view filtered to the active version, and
 * INSTEAD OF triggers that forward writes to state_by_version (which proxies to the vtable).
 */
export function applyStateView(args) {
    args.engine.sqlite.exec(`
    CREATE VIEW IF NOT EXISTS state AS
    SELECT 
      entity_id,
      schema_key,
      file_id,
      plugin_key,
      snapshot_content,
      schema_version,
      created_at,
      updated_at,
      inherited_from_version_id,
      change_id,
      untracked,
      commit_id,
      writer_key,
      metadata
    FROM state_by_version
    WHERE version_id IN (SELECT version_id FROM active_version);

    -- Forward writes to the active version via state_by_version
    CREATE TRIGGER IF NOT EXISTS state_insert
    INSTEAD OF INSERT ON state
    BEGIN
      INSERT INTO lix_internal_state_vtable (
        entity_id,
        schema_key,
        file_id,
        version_id,
        plugin_key,
        snapshot_content,
        schema_version,
        metadata,
        untracked
      ) VALUES (
        NEW.entity_id,
        NEW.schema_key,
        NEW.file_id,
        (SELECT version_id FROM active_version),
        NEW.plugin_key,
        NEW.snapshot_content,
        NEW.schema_version,
        NEW.metadata,
        COALESCE(NEW.untracked, 0)
      );
    END;

    CREATE TRIGGER IF NOT EXISTS state_update
    INSTEAD OF UPDATE ON state
    BEGIN
      UPDATE lix_internal_state_vtable
      SET
        entity_id = NEW.entity_id,
        schema_key = NEW.schema_key,
        file_id = NEW.file_id,
        plugin_key = NEW.plugin_key,
        snapshot_content = NEW.snapshot_content,
        schema_version = NEW.schema_version,
        metadata = NEW.metadata,
        untracked = COALESCE(NEW.untracked, 0)
      WHERE
        entity_id = OLD.entity_id
        AND schema_key = OLD.schema_key
        AND file_id = OLD.file_id
        AND version_id = (SELECT version_id FROM active_version);
    END;

    CREATE TRIGGER IF NOT EXISTS state_delete
    INSTEAD OF DELETE ON state
    BEGIN
      DELETE FROM lix_internal_state_vtable
      WHERE 
        entity_id = OLD.entity_id
        AND schema_key = OLD.schema_key
        AND file_id = OLD.file_id
        AND version_id = (SELECT version_id FROM active_version);
    END;
  `);
}
//# sourceMappingURL=state.js.map