package config

import (
	
	
	
	
	

	
	
	
	
	
	
)

// WorkConfig configures go.work-based workspace loading.
type WorkConfig struct {
	File string `json:"file,omitzero"`
}

// WalkConfig configures VCS tree-walk workspace loading.
type WalkConfig struct{}

// VCSConfig is an externally tagged VCS configuration.
type VCSConfig struct {
	Type   string
	Git    gitvcs.Config
	Hg     hgvcs.Config
	Fossil fossilvcs.Config
	JJ     jjvcs.Config
}

// IsZero reports whether no VCS variant is selected.
func ( VCSConfig) () bool { return .Type == "" }

// Name returns the selected VCS backend name (vcs.BackendAuto when unset).
func ( VCSConfig) () string { return cmp.Or(.Type, vcs.BackendAuto) }

// UnmarshalJSONFrom implements [json.UnmarshalerFrom].
func ( *VCSConfig) ( *jsontext.Decoder) error {
	* = VCSConfig{}
	,  := readExternalTag()
	if  != nil {
		return fmt.Errorf("vcs: %w", )
	}
	,  := .Unwrap()
	if ! {
		return nil
	}

	switch  := .String();  {
	case vcs.BackendAuto:
		.Type = vcs.BackendAuto
		var  struct{}
		if  := json.UnmarshalDecode(, &, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	case gitvcs.Name:
		.Type = gitvcs.Name
		if  := json.UnmarshalDecode(, &.Git, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	case hgvcs.Name:
		.Type = hgvcs.Name
		if  := json.UnmarshalDecode(, &.Hg, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	case fossilvcs.Name:
		.Type = fossilvcs.Name
		if  := json.UnmarshalDecode(, &.Fossil, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	case jjvcs.Name:
		.Type = jjvcs.Name
		if  := json.UnmarshalDecode(, &.JJ, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	default:
		return fmt.Errorf("unknown vcs %q", strings.Clone())
	}

	if  := readExternalTagEnd();  != nil {
		return fmt.Errorf("vcs: %w", )
	}
	return nil
}

// MarshalJSONTo implements [json.MarshalerTo].
func ( VCSConfig) ( *jsontext.Encoder) error {
	switch .Type {
	case gitvcs.Name:
		return marshalExternalTag(, gitvcs.Name, .Git)
	case hgvcs.Name:
		return marshalExternalTag(, hgvcs.Name, .Hg)
	case fossilvcs.Name:
		return marshalExternalTag(, fossilvcs.Name, .Fossil)
	case jjvcs.Name:
		return marshalExternalTag(, jjvcs.Name, .JJ)
	default:
		return marshalExternalTag(, vcs.BackendAuto, struct{}{})
	}
}

// WorkspaceConfig is an externally tagged workspace configuration.
type WorkspaceConfig struct {
	Type workspace.Mode
	Auto WorkConfig
	Work WorkConfig
	Walk WalkConfig
}

// IsZero reports whether no workspace variant is selected.
func ( WorkspaceConfig) () bool { return .Type == "" }

// Mode returns the selected workspace mode (workspace.ModeAuto when unset).
func ( WorkspaceConfig) () workspace.Mode { return cmp.Or(.Type, workspace.ModeAuto) }

// WorkFile returns the configured go.work file path, if any.
func ( WorkspaceConfig) () string {
	switch .Type {
	case workspace.ModeWork:
		return .Work.File
	case workspace.ModeAuto:
		return .Auto.File
	default:
		return ""
	}
}

// UnmarshalJSONFrom implements [json.UnmarshalerFrom].
func ( *WorkspaceConfig) ( *jsontext.Decoder) error {
	* = WorkspaceConfig{}
	,  := readExternalTag()
	if  != nil {
		return fmt.Errorf("workspace: %w", )
	}
	,  := .Unwrap()
	if ! {
		return nil
	}

	switch  := .String(); workspace.Mode() {
	case workspace.ModeAuto:
		.Type = workspace.ModeAuto
		if  := json.UnmarshalDecode(, &.Auto, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	case workspace.ModeWork:
		.Type = workspace.ModeWork
		if  := json.UnmarshalDecode(, &.Work, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	case workspace.ModeWalk:
		.Type = workspace.ModeWalk
		if  := json.UnmarshalDecode(, &.Walk, json.RejectUnknownMembers(true));  != nil {
			return 
		}
	default:
		return fmt.Errorf("unknown workspace mode %q", strings.Clone())
	}

	if  := readExternalTagEnd();  != nil {
		return fmt.Errorf("workspace: %w", )
	}
	return nil
}

// MarshalJSONTo implements [json.MarshalerTo].
func ( WorkspaceConfig) ( *jsontext.Encoder) error {
	switch .Type {
	case workspace.ModeWork:
		return marshalExternalTag(, string(workspace.ModeWork), .Work)
	case workspace.ModeWalk:
		return marshalExternalTag(, string(workspace.ModeWalk), .Walk)
	default:
		return marshalExternalTag(, string(workspace.ModeAuto), .Auto)
	}
}

func ( *jsontext.Encoder,  string,  any) error {
	if  := .WriteToken(jsontext.BeginObject);  != nil {
		return 
	}
	if  := .WriteToken(jsontext.String());  != nil {
		return 
	}
	if  := json.MarshalEncode(, );  != nil {
		return 
	}
	return .WriteToken(jsontext.EndObject)
}