# Tests various patterns of insertion of conjuncts.
# See https://cuelang.org/issues/2169
-- a.cue --
embeddingDirect: t1: ({
	#a: _
	_
} & {
	#a: "s"
	#a
})

embeddingExpr: t1: ({
	#a: _
	_
} & {
	#a: "s"
	[#a][0]
})

unifiedDirect: {
	t1: {
		#a: _
		_
	}
	t1: {
		#a
		#a: _
	}
	t1: {
		_
		#a: "s"
	}
}

unifiedExpr: {
	t1: {
		#a: _
		_
	}
	t1: {
		[#a][0]
		#a: _
	}
	t1: {
		#a: "s"
		_
	}
}

// These two embeddings both add fields to each other's original source, which
// then, in turn, needs to be added.
cross: t1: {
	a: {
		b: {v: 1, a: w: 2}
		// from b.a: {x: 2, b: y: 5}
	}
	b: {
		a: {x: 2, b: y: 5}
		// from a.b: {v: 1, a: v: 2}
	}
	a
	b
}
-- complex.cue --
// Multiple levels of insertion
recursive: {
	t1: {
		e: {}
		c: d: e: f: 1
		c.d
		c: d: {
			// This part gets embedded within t1, which ends up adding new fields
			// to here, when then again need to be embedded in t1, which then
			// will add more fields to node e.
			c: d: e: g: 1
		}
	}
	t2: {
		c
		c: c: g: 1
	}
}
-- issue2169.cue --
// Evaluator regression where "cannot add field $X: was already used" errors
// appeared incorrectly when a field was referenced before being defined.
// https://cuelang.org/issue/2169
embeddedFunctionalExpr: {
	#fn: {
		#in: _
		_
	} & {
		#in: "str"

		({
			#t: _
			#t
		} & {
			#t: #in
			_
		}) + ""
	}
	#fn
}
-- out/compile --
--- a.cue
{
  embeddingDirect: {
    t1: ({
      #a: _
      _
    } & {
      #a: "s"
      〈0;#a〉
    })
  }
  embeddingExpr: {
    t1: ({
      #a: _
      _
    } & {
      #a: "s"
      [
        〈1;#a〉,
      ][0]
    })
  }
  unifiedDirect: {
    t1: {
      #a: _
      _
    }
    t1: {
      〈0;#a〉
      #a: _
    }
    t1: {
      _
      #a: "s"
    }
  }
  unifiedExpr: {
    t1: {
      #a: _
      _
    }
    t1: {
      [
        〈1;#a〉,
      ][0]
      #a: _
    }
    t1: {
      #a: "s"
      _
    }
  }
  cross: {
    t1: {
      a: {
        b: {
          v: 1
          a: {
            w: 2
          }
        }
      }
      b: {
        a: {
          x: 2
          b: {
            y: 5
          }
        }
      }
      〈0;a〉
      〈0;b〉
    }
  }
}
--- complex.cue
{
  recursive: {
    t1: {
      e: {}
      c: {
        d: {
          e: {
            f: 1
          }
        }
      }
      〈0;c〉.d
      c: {
        d: {
          c: {
            d: {
              e: {
                g: 1
              }
            }
          }
        }
      }
    }
    t2: {
      〈0;c〉
      c: {
        c: {
          g: 1
        }
      }
    }
  }
}
--- issue2169.cue
{
  embeddedFunctionalExpr: {
    #fn: ({
      #in: _
      _
    } & {
      #in: "str"
      (({
        #t: _
        〈0;#t〉
      } & {
        #t: 〈1;#in〉
        _
      }) + "")
    })
    〈0;#fn〉
  }
}
