// deltablue.cm — Constraint solver kernel (DeltaBlue-inspired)
// Dynamic dispatch, pointer chasing, object-heavy workload.

def REQUIRED = 0
def STRONG = 1
def NORMAL = 2
def WEAK = 3
def WEAKEST = 4

function make_variable(name, value) {
  return {
    name: name,
    value: value,
    constraints: [],
    determined_by: null,
    stay: true,
    mark: 0
  }
}

function make_constraint(strength, variables, satisfy_fn) {
  return {
    strength: strength,
    variables: variables,
    satisfy: satisfy_fn,
    output: null
  }
}

// Constraint propagation: simple forward solver
function propagate(vars, constraints) {
  var changed = true
  var passes = 0
  var max_passes = length(constraints) * 3
  var i = 0
  var c = null
  var old_val = 0

  while (changed && passes < max_passes) {
    changed = false
    passes++
    for (i = 0; i < length(constraints); i++) {
      c = constraints[i]
      old_val = c.output ? c.output.value : null
      c.satisfy(c)
      if (c.output && c.output.value != old_val) {
        changed = true
      }
    }
  }
  return passes
}

// Build a chain of equality constraints: v[i] = v[i-1] + 1
function build_chain(n) {
  var vars = []
  var constraints = []
  var i = 0
  for (i = 0; i < n; i++) {
    push(vars, make_variable(`v${i}`, 0))
  }

  // Set first variable
  vars[0].value = 1

  var c = null
  for (i = 1; i < n; i++) {
    c = make_constraint(NORMAL, [vars[i - 1], vars[i]], function(self) {
      self.variables[1].value = self.variables[0].value + 1
      self.output = self.variables[1]
    })
    push(constraints, c)
    push(vars[i].constraints, c)
  }

  return {vars: vars, constraints: constraints}
}

// Build a projection: pairs of variables with scaling constraints
function build_projection(n) {
  var src = []
  var dst = []
  var constraints = []
  var i = 0
  for (i = 0; i < n; i++) {
    push(src, make_variable(`src${i}`, i * 10))
    push(dst, make_variable(`dst${i}`, 0))
  }

  var scale_c = null
  for (i = 0; i < n; i++) {
    scale_c = make_constraint(STRONG, [src[i], dst[i]], function(self) {
      self.variables[1].value = self.variables[0].value * 2 + 1
      self.output = self.variables[1]
    })
    push(constraints, scale_c)
    push(dst[i].constraints, scale_c)
  }

  return {src: src, dst: dst, constraints: constraints}
}

// Edit constraint: change a source, re-propagate
function run_edits(system, edits) {
  var i = 0
  var total_passes = 0
  for (i = 0; i < edits; i++) {
    system.vars[0].value = i
    total_passes += propagate(system.vars, system.constraints)
  }
  return total_passes
}

return {
  // Chain of 100 variables, propagate
  chain_100: function(n) {
    var i = 0
    var chain = null
    var x = 0
    for (i = 0; i < n; i++) {
      chain = build_chain(100)
      x += propagate(chain.vars, chain.constraints)
    }
    return x
  },

  // Chain of 500 variables, propagate
  chain_500: function(n) {
    var i = 0
    var chain = null
    var x = 0
    for (i = 0; i < n; i++) {
      chain = build_chain(500)
      x += propagate(chain.vars, chain.constraints)
    }
    return x
  },

  // Projection of 100 pairs
  projection_100: function(n) {
    var i = 0
    var proj = null
    var x = 0
    for (i = 0; i < n; i++) {
      proj = build_projection(100)
      x += propagate(proj.src, proj.constraints)
    }
    return x
  },

  // Edit and re-propagate (incremental update)
  chain_edit_100: function(n) {
    var chain = build_chain(100)
    var i = 0
    var x = 0
    for (i = 0; i < n; i++) {
      chain.vars[0].value = i
      x += propagate(chain.vars, chain.constraints)
    }
    return x
  }
}
