package memvcs

import (
	
	
	
	
	
	
	

	
)

type session struct {
	repo      *Repo
	rev       vcs.Revision
	head      string
	seq       int
	workFiles map[string][]byte
	headFiles map[string][]byte
	staged    map[string]Commit
	tags      map[string]string
	tagOrder  []string
}

func ( *session) () fs.FS {
	 := make(fstest.MapFS, len(.workFiles))
	for ,  := range .workFiles {
		[] = &fstest.MapFile{
			Data: bytes.Clone(),
			Mode: fs.FileMode(0o644),
		}
	}
	return 
}

func ( *session) ( string,  []byte) error {
	.workFiles[cleanPath()] = bytes.Clone()
	return nil
}

func ( *session) ( context.Context) (string, error) {
	return .head, nil
}

func ( *session) ( context.Context,  string) ([]vcs.FileEntry, error) {
	return listFromMap(.headFiles, ), nil
}

func ( *session) ( context.Context,  []string,  vcs.CommitOptions) (string, error) {
	if len() == 0 {
		return .head, nil
	}
	 := cloneFiles(.headFiles)
	for ,  := range  {
		 := cleanPath()
		,  := .workFiles[]
		if ! {
			return "", fmt.Errorf("memvcs: file %q not in working tree", )
		}
		[] = bytes.Clone()
	}
	.seq++
	 := fmt.Sprintf("mem-tx-%04d", .seq)
	.staged[] = Commit{
		ID:      ,
		Parent:  .head,
		Message: .Message,
		Files:   ,
	}
	.head = 
	.headFiles = 
	return , nil
}

func ( *session) ( context.Context,  string,  vcs.TagOptions) error {
	.tags[] = .head
	.tagOrder = append(.tagOrder, )
	return nil
}

func ( *session) ( context.Context) (vcs.PublishResult, error) {
	.repo.mu.Lock()
	defer .repo.mu.Unlock()

	if .rev.Ref != "" && .repo.refs[.rev.Ref] != .rev.ID {
		return vcs.PublishResult{}, fmt.Errorf("memvcs: ref %q moved during release", .rev.Ref)
	}
	for  := range .tags {
		if ,  := .repo.tags[];  {
			return vcs.PublishResult{}, fmt.Errorf("memvcs: tag %q already exists", )
		}
	}

	maps.Copy(.repo.commits, .staged)
	if .rev.Ref != "" && .head != .rev.ID {
		.repo.refs[.rev.Ref] = .head
	}
	maps.Copy(.repo.tags, .tags)

	return vcs.PublishResult{
		StartID: .rev.ID,
		FinalID: .head,
		Ref:     .rev.Ref,
		Label:   .rev.Label,
		Tags:    slices.Clone(.tagOrder),
	}, nil
}

func ( *session) () error { return nil }