package runsync

import spb "github.com/wandb/wandb/core/pkg/service_go_proto"

// RunSyncUpdates contains the updates to apply to a run when syncing it.
//
// A nil value makes no updates.
type RunSyncUpdates struct {
	// Changes to where to upload the run.
	//
	// Empty strings indicate no update.
	Entity, Project, RunID string
}

// UpdatesFromRequest constructs RunSyncUpdates from a sync init request.
func UpdatesFromRequest(request *spb.ServerInitSyncRequest) *RunSyncUpdates {
	u := &RunSyncUpdates{}

	if entity := request.GetNewEntity(); entity != "" {
		u.Entity = entity
	}
	if project := request.GetNewProject(); project != "" {
		u.Project = project
	}
	if runID := request.GetNewRunId(); runID != "" {
		u.RunID = runID
	}

	return u
}

// Modify updates a record with modifications requested for syncing.
func (u *RunSyncUpdates) Modify(record *spb.Record) {
	if u == nil {
		return
	}

	if run := record.GetRun(); run != nil {
		if u.Entity != "" {
			run.Entity = u.Entity
		}

		if u.Project != "" {
			run.Project = u.Project
		}

		if u.RunID != "" {
			run.RunId = u.RunID
		}
	}
}
