package session

import (
	
	
	
	

	
)

// StorageMemory implements in-memory session storage.
// Goroutine-safe.
type StorageMemory struct {
	mux  sync.RWMutex
	data []byte
}

// Dump dumps raw session data to the given writer.
// Returns ErrNotFound if storage is nil or if underlying session is empty.
func ( *StorageMemory) ( io.Writer) error {
	if  == nil {
		return ErrNotFound
	}
	.mux.RLock()
	defer .mux.RUnlock()

	if len(.data) == 0 {
		return ErrNotFound
	}

	if ,  := .Write(.data);  != nil {
		return errors.Wrap(, "write session")
	}

	return nil
}

// WriteFile dumps raw session data to the named file, creating it if necessary.
// Returns ErrNotFound if storage is nil or if underlying session is empty.
func ( *StorageMemory) ( string,  os.FileMode) error {
	,  := .Bytes(nil)
	if  != nil {
		return 
	}
	return os.WriteFile(, , )
}

// Bytes appends raw session data to the given slice.
// Returns ErrNotFound if storage is nil or if underlying session is empty.
func ( *StorageMemory) ( []byte) ([]byte, error) {
	if  == nil {
		return nil, ErrNotFound
	}
	.mux.RLock()
	defer .mux.RUnlock()

	if len(.data) == 0 {
		return nil, ErrNotFound
	}

	return append(, .data...), nil
}

// Clone creates a clone of existing StorageMemory,
func ( *StorageMemory) () *StorageMemory {
	 := &StorageMemory{}

	.data, _ = .Bytes(.data)
	return 
}

// LoadSession loads session from memory.
func ( *StorageMemory) (context.Context) ([]byte, error) {
	if  == nil {
		return nil, ErrNotFound
	}
	.mux.RLock()
	defer .mux.RUnlock()

	if len(.data) == 0 {
		return nil, ErrNotFound
	}
	 := append([]byte(nil), .data...)

	return , nil
}

// StoreSession stores session to memory.
func ( *StorageMemory) ( context.Context,  []byte) error {
	if  == nil {
		return errors.New("StoreSession called on StorageMemory(nil)")
	}

	.mux.Lock()
	.data = 
	.mux.Unlock()
	return nil
}