// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/so/ent/depositaddress"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/tree"
	"github.com/lightsparkdev/spark/so/ent/utxo"
	"github.com/lightsparkdev/spark/so/ent/utxoswap"
)

// DepositAddressUpdate is the builder for updating DepositAddress entities.
type DepositAddressUpdate struct {
	config
	hooks    []Hook
	mutation *DepositAddressMutation
}

// Where appends a list predicates to the DepositAddressUpdate builder.
func (dau *DepositAddressUpdate) Where(ps ...predicate.DepositAddress) *DepositAddressUpdate {
	dau.mutation.Where(ps...)
	return dau
}

// SetUpdateTime sets the "update_time" field.
func (dau *DepositAddressUpdate) SetUpdateTime(t time.Time) *DepositAddressUpdate {
	dau.mutation.SetUpdateTime(t)
	return dau
}

// SetConfirmationHeight sets the "confirmation_height" field.
func (dau *DepositAddressUpdate) SetConfirmationHeight(i int64) *DepositAddressUpdate {
	dau.mutation.ResetConfirmationHeight()
	dau.mutation.SetConfirmationHeight(i)
	return dau
}

// SetNillableConfirmationHeight sets the "confirmation_height" field if the given value is not nil.
func (dau *DepositAddressUpdate) SetNillableConfirmationHeight(i *int64) *DepositAddressUpdate {
	if i != nil {
		dau.SetConfirmationHeight(*i)
	}
	return dau
}

// AddConfirmationHeight adds i to the "confirmation_height" field.
func (dau *DepositAddressUpdate) AddConfirmationHeight(i int64) *DepositAddressUpdate {
	dau.mutation.AddConfirmationHeight(i)
	return dau
}

// ClearConfirmationHeight clears the value of the "confirmation_height" field.
func (dau *DepositAddressUpdate) ClearConfirmationHeight() *DepositAddressUpdate {
	dau.mutation.ClearConfirmationHeight()
	return dau
}

// SetConfirmationTxid sets the "confirmation_txid" field.
func (dau *DepositAddressUpdate) SetConfirmationTxid(s string) *DepositAddressUpdate {
	dau.mutation.SetConfirmationTxid(s)
	return dau
}

// SetNillableConfirmationTxid sets the "confirmation_txid" field if the given value is not nil.
func (dau *DepositAddressUpdate) SetNillableConfirmationTxid(s *string) *DepositAddressUpdate {
	if s != nil {
		dau.SetConfirmationTxid(*s)
	}
	return dau
}

// ClearConfirmationTxid clears the value of the "confirmation_txid" field.
func (dau *DepositAddressUpdate) ClearConfirmationTxid() *DepositAddressUpdate {
	dau.mutation.ClearConfirmationTxid()
	return dau
}

// SetAddressSignatures sets the "address_signatures" field.
func (dau *DepositAddressUpdate) SetAddressSignatures(m map[string][]uint8) *DepositAddressUpdate {
	dau.mutation.SetAddressSignatures(m)
	return dau
}

// ClearAddressSignatures clears the value of the "address_signatures" field.
func (dau *DepositAddressUpdate) ClearAddressSignatures() *DepositAddressUpdate {
	dau.mutation.ClearAddressSignatures()
	return dau
}

// SetPossessionSignature sets the "possession_signature" field.
func (dau *DepositAddressUpdate) SetPossessionSignature(b []byte) *DepositAddressUpdate {
	dau.mutation.SetPossessionSignature(b)
	return dau
}

// ClearPossessionSignature clears the value of the "possession_signature" field.
func (dau *DepositAddressUpdate) ClearPossessionSignature() *DepositAddressUpdate {
	dau.mutation.ClearPossessionSignature()
	return dau
}

// SetNodeID sets the "node_id" field.
func (dau *DepositAddressUpdate) SetNodeID(u uuid.UUID) *DepositAddressUpdate {
	dau.mutation.SetNodeID(u)
	return dau
}

// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (dau *DepositAddressUpdate) SetNillableNodeID(u *uuid.UUID) *DepositAddressUpdate {
	if u != nil {
		dau.SetNodeID(*u)
	}
	return dau
}

// ClearNodeID clears the value of the "node_id" field.
func (dau *DepositAddressUpdate) ClearNodeID() *DepositAddressUpdate {
	dau.mutation.ClearNodeID()
	return dau
}

// SetIsStatic sets the "is_static" field.
func (dau *DepositAddressUpdate) SetIsStatic(b bool) *DepositAddressUpdate {
	dau.mutation.SetIsStatic(b)
	return dau
}

// SetNillableIsStatic sets the "is_static" field if the given value is not nil.
func (dau *DepositAddressUpdate) SetNillableIsStatic(b *bool) *DepositAddressUpdate {
	if b != nil {
		dau.SetIsStatic(*b)
	}
	return dau
}

// SetIsDefault sets the "is_default" field.
func (dau *DepositAddressUpdate) SetIsDefault(b bool) *DepositAddressUpdate {
	dau.mutation.SetIsDefault(b)
	return dau
}

// SetNillableIsDefault sets the "is_default" field if the given value is not nil.
func (dau *DepositAddressUpdate) SetNillableIsDefault(b *bool) *DepositAddressUpdate {
	if b != nil {
		dau.SetIsDefault(*b)
	}
	return dau
}

// AddUtxoIDs adds the "utxo" edge to the Utxo entity by IDs.
func (dau *DepositAddressUpdate) AddUtxoIDs(ids ...uuid.UUID) *DepositAddressUpdate {
	dau.mutation.AddUtxoIDs(ids...)
	return dau
}

// AddUtxo adds the "utxo" edges to the Utxo entity.
func (dau *DepositAddressUpdate) AddUtxo(u ...*Utxo) *DepositAddressUpdate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dau.AddUtxoIDs(ids...)
}

// AddUtxoswapIDs adds the "utxoswaps" edge to the UtxoSwap entity by IDs.
func (dau *DepositAddressUpdate) AddUtxoswapIDs(ids ...uuid.UUID) *DepositAddressUpdate {
	dau.mutation.AddUtxoswapIDs(ids...)
	return dau
}

// AddUtxoswaps adds the "utxoswaps" edges to the UtxoSwap entity.
func (dau *DepositAddressUpdate) AddUtxoswaps(u ...*UtxoSwap) *DepositAddressUpdate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dau.AddUtxoswapIDs(ids...)
}

// SetTreeID sets the "tree" edge to the Tree entity by ID.
func (dau *DepositAddressUpdate) SetTreeID(id uuid.UUID) *DepositAddressUpdate {
	dau.mutation.SetTreeID(id)
	return dau
}

// SetNillableTreeID sets the "tree" edge to the Tree entity by ID if the given value is not nil.
func (dau *DepositAddressUpdate) SetNillableTreeID(id *uuid.UUID) *DepositAddressUpdate {
	if id != nil {
		dau = dau.SetTreeID(*id)
	}
	return dau
}

// SetTree sets the "tree" edge to the Tree entity.
func (dau *DepositAddressUpdate) SetTree(t *Tree) *DepositAddressUpdate {
	return dau.SetTreeID(t.ID)
}

// Mutation returns the DepositAddressMutation object of the builder.
func (dau *DepositAddressUpdate) Mutation() *DepositAddressMutation {
	return dau.mutation
}

// ClearUtxo clears all "utxo" edges to the Utxo entity.
func (dau *DepositAddressUpdate) ClearUtxo() *DepositAddressUpdate {
	dau.mutation.ClearUtxo()
	return dau
}

// RemoveUtxoIDs removes the "utxo" edge to Utxo entities by IDs.
func (dau *DepositAddressUpdate) RemoveUtxoIDs(ids ...uuid.UUID) *DepositAddressUpdate {
	dau.mutation.RemoveUtxoIDs(ids...)
	return dau
}

// RemoveUtxo removes "utxo" edges to Utxo entities.
func (dau *DepositAddressUpdate) RemoveUtxo(u ...*Utxo) *DepositAddressUpdate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dau.RemoveUtxoIDs(ids...)
}

// ClearUtxoswaps clears all "utxoswaps" edges to the UtxoSwap entity.
func (dau *DepositAddressUpdate) ClearUtxoswaps() *DepositAddressUpdate {
	dau.mutation.ClearUtxoswaps()
	return dau
}

// RemoveUtxoswapIDs removes the "utxoswaps" edge to UtxoSwap entities by IDs.
func (dau *DepositAddressUpdate) RemoveUtxoswapIDs(ids ...uuid.UUID) *DepositAddressUpdate {
	dau.mutation.RemoveUtxoswapIDs(ids...)
	return dau
}

// RemoveUtxoswaps removes "utxoswaps" edges to UtxoSwap entities.
func (dau *DepositAddressUpdate) RemoveUtxoswaps(u ...*UtxoSwap) *DepositAddressUpdate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dau.RemoveUtxoswapIDs(ids...)
}

// ClearTree clears the "tree" edge to the Tree entity.
func (dau *DepositAddressUpdate) ClearTree() *DepositAddressUpdate {
	dau.mutation.ClearTree()
	return dau
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (dau *DepositAddressUpdate) Save(ctx context.Context) (int, error) {
	if err := dau.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, dau.sqlSave, dau.mutation, dau.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (dau *DepositAddressUpdate) SaveX(ctx context.Context) int {
	affected, err := dau.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (dau *DepositAddressUpdate) Exec(ctx context.Context) error {
	_, err := dau.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (dau *DepositAddressUpdate) ExecX(ctx context.Context) {
	if err := dau.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (dau *DepositAddressUpdate) defaults() error {
	if _, ok := dau.mutation.UpdateTime(); !ok {
		if depositaddress.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized depositaddress.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := depositaddress.UpdateDefaultUpdateTime()
		dau.mutation.SetUpdateTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (dau *DepositAddressUpdate) check() error {
	if dau.mutation.SigningKeyshareCleared() && len(dau.mutation.SigningKeyshareIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "DepositAddress.signing_keyshare"`)
	}
	return nil
}

func (dau *DepositAddressUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := dau.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(depositaddress.Table, depositaddress.Columns, sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID))
	if ps := dau.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dau.mutation.UpdateTime(); ok {
		_spec.SetField(depositaddress.FieldUpdateTime, field.TypeTime, value)
	}
	if dau.mutation.NetworkCleared() {
		_spec.ClearField(depositaddress.FieldNetwork, field.TypeEnum)
	}
	if value, ok := dau.mutation.ConfirmationHeight(); ok {
		_spec.SetField(depositaddress.FieldConfirmationHeight, field.TypeInt64, value)
	}
	if value, ok := dau.mutation.AddedConfirmationHeight(); ok {
		_spec.AddField(depositaddress.FieldConfirmationHeight, field.TypeInt64, value)
	}
	if dau.mutation.ConfirmationHeightCleared() {
		_spec.ClearField(depositaddress.FieldConfirmationHeight, field.TypeInt64)
	}
	if value, ok := dau.mutation.ConfirmationTxid(); ok {
		_spec.SetField(depositaddress.FieldConfirmationTxid, field.TypeString, value)
	}
	if dau.mutation.ConfirmationTxidCleared() {
		_spec.ClearField(depositaddress.FieldConfirmationTxid, field.TypeString)
	}
	if value, ok := dau.mutation.AddressSignatures(); ok {
		_spec.SetField(depositaddress.FieldAddressSignatures, field.TypeJSON, value)
	}
	if dau.mutation.AddressSignaturesCleared() {
		_spec.ClearField(depositaddress.FieldAddressSignatures, field.TypeJSON)
	}
	if value, ok := dau.mutation.PossessionSignature(); ok {
		_spec.SetField(depositaddress.FieldPossessionSignature, field.TypeBytes, value)
	}
	if dau.mutation.PossessionSignatureCleared() {
		_spec.ClearField(depositaddress.FieldPossessionSignature, field.TypeBytes)
	}
	if value, ok := dau.mutation.NodeID(); ok {
		_spec.SetField(depositaddress.FieldNodeID, field.TypeUUID, value)
	}
	if dau.mutation.NodeIDCleared() {
		_spec.ClearField(depositaddress.FieldNodeID, field.TypeUUID)
	}
	if value, ok := dau.mutation.IsStatic(); ok {
		_spec.SetField(depositaddress.FieldIsStatic, field.TypeBool, value)
	}
	if value, ok := dau.mutation.IsDefault(); ok {
		_spec.SetField(depositaddress.FieldIsDefault, field.TypeBool, value)
	}
	if dau.mutation.UtxoCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoTable,
			Columns: []string{depositaddress.UtxoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dau.mutation.RemovedUtxoIDs(); len(nodes) > 0 && !dau.mutation.UtxoCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoTable,
			Columns: []string{depositaddress.UtxoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dau.mutation.UtxoIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoTable,
			Columns: []string{depositaddress.UtxoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if dau.mutation.UtxoswapsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoswapsTable,
			Columns: []string{depositaddress.UtxoswapsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxoswap.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dau.mutation.RemovedUtxoswapsIDs(); len(nodes) > 0 && !dau.mutation.UtxoswapsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoswapsTable,
			Columns: []string{depositaddress.UtxoswapsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxoswap.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dau.mutation.UtxoswapsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoswapsTable,
			Columns: []string{depositaddress.UtxoswapsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxoswap.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if dau.mutation.TreeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   depositaddress.TreeTable,
			Columns: []string{depositaddress.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dau.mutation.TreeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   depositaddress.TreeTable,
			Columns: []string{depositaddress.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, dau.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{depositaddress.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	dau.mutation.done = true
	return n, nil
}

// DepositAddressUpdateOne is the builder for updating a single DepositAddress entity.
type DepositAddressUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DepositAddressMutation
}

// SetUpdateTime sets the "update_time" field.
func (dauo *DepositAddressUpdateOne) SetUpdateTime(t time.Time) *DepositAddressUpdateOne {
	dauo.mutation.SetUpdateTime(t)
	return dauo
}

// SetConfirmationHeight sets the "confirmation_height" field.
func (dauo *DepositAddressUpdateOne) SetConfirmationHeight(i int64) *DepositAddressUpdateOne {
	dauo.mutation.ResetConfirmationHeight()
	dauo.mutation.SetConfirmationHeight(i)
	return dauo
}

// SetNillableConfirmationHeight sets the "confirmation_height" field if the given value is not nil.
func (dauo *DepositAddressUpdateOne) SetNillableConfirmationHeight(i *int64) *DepositAddressUpdateOne {
	if i != nil {
		dauo.SetConfirmationHeight(*i)
	}
	return dauo
}

// AddConfirmationHeight adds i to the "confirmation_height" field.
func (dauo *DepositAddressUpdateOne) AddConfirmationHeight(i int64) *DepositAddressUpdateOne {
	dauo.mutation.AddConfirmationHeight(i)
	return dauo
}

// ClearConfirmationHeight clears the value of the "confirmation_height" field.
func (dauo *DepositAddressUpdateOne) ClearConfirmationHeight() *DepositAddressUpdateOne {
	dauo.mutation.ClearConfirmationHeight()
	return dauo
}

// SetConfirmationTxid sets the "confirmation_txid" field.
func (dauo *DepositAddressUpdateOne) SetConfirmationTxid(s string) *DepositAddressUpdateOne {
	dauo.mutation.SetConfirmationTxid(s)
	return dauo
}

// SetNillableConfirmationTxid sets the "confirmation_txid" field if the given value is not nil.
func (dauo *DepositAddressUpdateOne) SetNillableConfirmationTxid(s *string) *DepositAddressUpdateOne {
	if s != nil {
		dauo.SetConfirmationTxid(*s)
	}
	return dauo
}

// ClearConfirmationTxid clears the value of the "confirmation_txid" field.
func (dauo *DepositAddressUpdateOne) ClearConfirmationTxid() *DepositAddressUpdateOne {
	dauo.mutation.ClearConfirmationTxid()
	return dauo
}

// SetAddressSignatures sets the "address_signatures" field.
func (dauo *DepositAddressUpdateOne) SetAddressSignatures(m map[string][]uint8) *DepositAddressUpdateOne {
	dauo.mutation.SetAddressSignatures(m)
	return dauo
}

// ClearAddressSignatures clears the value of the "address_signatures" field.
func (dauo *DepositAddressUpdateOne) ClearAddressSignatures() *DepositAddressUpdateOne {
	dauo.mutation.ClearAddressSignatures()
	return dauo
}

// SetPossessionSignature sets the "possession_signature" field.
func (dauo *DepositAddressUpdateOne) SetPossessionSignature(b []byte) *DepositAddressUpdateOne {
	dauo.mutation.SetPossessionSignature(b)
	return dauo
}

// ClearPossessionSignature clears the value of the "possession_signature" field.
func (dauo *DepositAddressUpdateOne) ClearPossessionSignature() *DepositAddressUpdateOne {
	dauo.mutation.ClearPossessionSignature()
	return dauo
}

// SetNodeID sets the "node_id" field.
func (dauo *DepositAddressUpdateOne) SetNodeID(u uuid.UUID) *DepositAddressUpdateOne {
	dauo.mutation.SetNodeID(u)
	return dauo
}

// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (dauo *DepositAddressUpdateOne) SetNillableNodeID(u *uuid.UUID) *DepositAddressUpdateOne {
	if u != nil {
		dauo.SetNodeID(*u)
	}
	return dauo
}

// ClearNodeID clears the value of the "node_id" field.
func (dauo *DepositAddressUpdateOne) ClearNodeID() *DepositAddressUpdateOne {
	dauo.mutation.ClearNodeID()
	return dauo
}

// SetIsStatic sets the "is_static" field.
func (dauo *DepositAddressUpdateOne) SetIsStatic(b bool) *DepositAddressUpdateOne {
	dauo.mutation.SetIsStatic(b)
	return dauo
}

// SetNillableIsStatic sets the "is_static" field if the given value is not nil.
func (dauo *DepositAddressUpdateOne) SetNillableIsStatic(b *bool) *DepositAddressUpdateOne {
	if b != nil {
		dauo.SetIsStatic(*b)
	}
	return dauo
}

// SetIsDefault sets the "is_default" field.
func (dauo *DepositAddressUpdateOne) SetIsDefault(b bool) *DepositAddressUpdateOne {
	dauo.mutation.SetIsDefault(b)
	return dauo
}

// SetNillableIsDefault sets the "is_default" field if the given value is not nil.
func (dauo *DepositAddressUpdateOne) SetNillableIsDefault(b *bool) *DepositAddressUpdateOne {
	if b != nil {
		dauo.SetIsDefault(*b)
	}
	return dauo
}

// AddUtxoIDs adds the "utxo" edge to the Utxo entity by IDs.
func (dauo *DepositAddressUpdateOne) AddUtxoIDs(ids ...uuid.UUID) *DepositAddressUpdateOne {
	dauo.mutation.AddUtxoIDs(ids...)
	return dauo
}

// AddUtxo adds the "utxo" edges to the Utxo entity.
func (dauo *DepositAddressUpdateOne) AddUtxo(u ...*Utxo) *DepositAddressUpdateOne {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dauo.AddUtxoIDs(ids...)
}

// AddUtxoswapIDs adds the "utxoswaps" edge to the UtxoSwap entity by IDs.
func (dauo *DepositAddressUpdateOne) AddUtxoswapIDs(ids ...uuid.UUID) *DepositAddressUpdateOne {
	dauo.mutation.AddUtxoswapIDs(ids...)
	return dauo
}

// AddUtxoswaps adds the "utxoswaps" edges to the UtxoSwap entity.
func (dauo *DepositAddressUpdateOne) AddUtxoswaps(u ...*UtxoSwap) *DepositAddressUpdateOne {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dauo.AddUtxoswapIDs(ids...)
}

// SetTreeID sets the "tree" edge to the Tree entity by ID.
func (dauo *DepositAddressUpdateOne) SetTreeID(id uuid.UUID) *DepositAddressUpdateOne {
	dauo.mutation.SetTreeID(id)
	return dauo
}

// SetNillableTreeID sets the "tree" edge to the Tree entity by ID if the given value is not nil.
func (dauo *DepositAddressUpdateOne) SetNillableTreeID(id *uuid.UUID) *DepositAddressUpdateOne {
	if id != nil {
		dauo = dauo.SetTreeID(*id)
	}
	return dauo
}

// SetTree sets the "tree" edge to the Tree entity.
func (dauo *DepositAddressUpdateOne) SetTree(t *Tree) *DepositAddressUpdateOne {
	return dauo.SetTreeID(t.ID)
}

// Mutation returns the DepositAddressMutation object of the builder.
func (dauo *DepositAddressUpdateOne) Mutation() *DepositAddressMutation {
	return dauo.mutation
}

// ClearUtxo clears all "utxo" edges to the Utxo entity.
func (dauo *DepositAddressUpdateOne) ClearUtxo() *DepositAddressUpdateOne {
	dauo.mutation.ClearUtxo()
	return dauo
}

// RemoveUtxoIDs removes the "utxo" edge to Utxo entities by IDs.
func (dauo *DepositAddressUpdateOne) RemoveUtxoIDs(ids ...uuid.UUID) *DepositAddressUpdateOne {
	dauo.mutation.RemoveUtxoIDs(ids...)
	return dauo
}

// RemoveUtxo removes "utxo" edges to Utxo entities.
func (dauo *DepositAddressUpdateOne) RemoveUtxo(u ...*Utxo) *DepositAddressUpdateOne {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dauo.RemoveUtxoIDs(ids...)
}

// ClearUtxoswaps clears all "utxoswaps" edges to the UtxoSwap entity.
func (dauo *DepositAddressUpdateOne) ClearUtxoswaps() *DepositAddressUpdateOne {
	dauo.mutation.ClearUtxoswaps()
	return dauo
}

// RemoveUtxoswapIDs removes the "utxoswaps" edge to UtxoSwap entities by IDs.
func (dauo *DepositAddressUpdateOne) RemoveUtxoswapIDs(ids ...uuid.UUID) *DepositAddressUpdateOne {
	dauo.mutation.RemoveUtxoswapIDs(ids...)
	return dauo
}

// RemoveUtxoswaps removes "utxoswaps" edges to UtxoSwap entities.
func (dauo *DepositAddressUpdateOne) RemoveUtxoswaps(u ...*UtxoSwap) *DepositAddressUpdateOne {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return dauo.RemoveUtxoswapIDs(ids...)
}

// ClearTree clears the "tree" edge to the Tree entity.
func (dauo *DepositAddressUpdateOne) ClearTree() *DepositAddressUpdateOne {
	dauo.mutation.ClearTree()
	return dauo
}

// Where appends a list predicates to the DepositAddressUpdate builder.
func (dauo *DepositAddressUpdateOne) Where(ps ...predicate.DepositAddress) *DepositAddressUpdateOne {
	dauo.mutation.Where(ps...)
	return dauo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (dauo *DepositAddressUpdateOne) Select(field string, fields ...string) *DepositAddressUpdateOne {
	dauo.fields = append([]string{field}, fields...)
	return dauo
}

// Save executes the query and returns the updated DepositAddress entity.
func (dauo *DepositAddressUpdateOne) Save(ctx context.Context) (*DepositAddress, error) {
	if err := dauo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, dauo.sqlSave, dauo.mutation, dauo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (dauo *DepositAddressUpdateOne) SaveX(ctx context.Context) *DepositAddress {
	node, err := dauo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (dauo *DepositAddressUpdateOne) Exec(ctx context.Context) error {
	_, err := dauo.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (dauo *DepositAddressUpdateOne) ExecX(ctx context.Context) {
	if err := dauo.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (dauo *DepositAddressUpdateOne) defaults() error {
	if _, ok := dauo.mutation.UpdateTime(); !ok {
		if depositaddress.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized depositaddress.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := depositaddress.UpdateDefaultUpdateTime()
		dauo.mutation.SetUpdateTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (dauo *DepositAddressUpdateOne) check() error {
	if dauo.mutation.SigningKeyshareCleared() && len(dauo.mutation.SigningKeyshareIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "DepositAddress.signing_keyshare"`)
	}
	return nil
}

func (dauo *DepositAddressUpdateOne) sqlSave(ctx context.Context) (_node *DepositAddress, err error) {
	if err := dauo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(depositaddress.Table, depositaddress.Columns, sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID))
	id, ok := dauo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "DepositAddress.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dauo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, depositaddress.FieldID)
		for _, f := range fields {
			if !depositaddress.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != depositaddress.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dauo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dauo.mutation.UpdateTime(); ok {
		_spec.SetField(depositaddress.FieldUpdateTime, field.TypeTime, value)
	}
	if dauo.mutation.NetworkCleared() {
		_spec.ClearField(depositaddress.FieldNetwork, field.TypeEnum)
	}
	if value, ok := dauo.mutation.ConfirmationHeight(); ok {
		_spec.SetField(depositaddress.FieldConfirmationHeight, field.TypeInt64, value)
	}
	if value, ok := dauo.mutation.AddedConfirmationHeight(); ok {
		_spec.AddField(depositaddress.FieldConfirmationHeight, field.TypeInt64, value)
	}
	if dauo.mutation.ConfirmationHeightCleared() {
		_spec.ClearField(depositaddress.FieldConfirmationHeight, field.TypeInt64)
	}
	if value, ok := dauo.mutation.ConfirmationTxid(); ok {
		_spec.SetField(depositaddress.FieldConfirmationTxid, field.TypeString, value)
	}
	if dauo.mutation.ConfirmationTxidCleared() {
		_spec.ClearField(depositaddress.FieldConfirmationTxid, field.TypeString)
	}
	if value, ok := dauo.mutation.AddressSignatures(); ok {
		_spec.SetField(depositaddress.FieldAddressSignatures, field.TypeJSON, value)
	}
	if dauo.mutation.AddressSignaturesCleared() {
		_spec.ClearField(depositaddress.FieldAddressSignatures, field.TypeJSON)
	}
	if value, ok := dauo.mutation.PossessionSignature(); ok {
		_spec.SetField(depositaddress.FieldPossessionSignature, field.TypeBytes, value)
	}
	if dauo.mutation.PossessionSignatureCleared() {
		_spec.ClearField(depositaddress.FieldPossessionSignature, field.TypeBytes)
	}
	if value, ok := dauo.mutation.NodeID(); ok {
		_spec.SetField(depositaddress.FieldNodeID, field.TypeUUID, value)
	}
	if dauo.mutation.NodeIDCleared() {
		_spec.ClearField(depositaddress.FieldNodeID, field.TypeUUID)
	}
	if value, ok := dauo.mutation.IsStatic(); ok {
		_spec.SetField(depositaddress.FieldIsStatic, field.TypeBool, value)
	}
	if value, ok := dauo.mutation.IsDefault(); ok {
		_spec.SetField(depositaddress.FieldIsDefault, field.TypeBool, value)
	}
	if dauo.mutation.UtxoCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoTable,
			Columns: []string{depositaddress.UtxoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dauo.mutation.RemovedUtxoIDs(); len(nodes) > 0 && !dauo.mutation.UtxoCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoTable,
			Columns: []string{depositaddress.UtxoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dauo.mutation.UtxoIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoTable,
			Columns: []string{depositaddress.UtxoColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if dauo.mutation.UtxoswapsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoswapsTable,
			Columns: []string{depositaddress.UtxoswapsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxoswap.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dauo.mutation.RemovedUtxoswapsIDs(); len(nodes) > 0 && !dauo.mutation.UtxoswapsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoswapsTable,
			Columns: []string{depositaddress.UtxoswapsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxoswap.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dauo.mutation.UtxoswapsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   depositaddress.UtxoswapsTable,
			Columns: []string{depositaddress.UtxoswapsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(utxoswap.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if dauo.mutation.TreeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   depositaddress.TreeTable,
			Columns: []string{depositaddress.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := dauo.mutation.TreeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   depositaddress.TreeTable,
			Columns: []string{depositaddress.TreeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &DepositAddress{config: dauo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dauo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{depositaddress.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dauo.mutation.done = true
	return _node, nil
}
