package vfs

import (
	
	

	

	
)

// Filename is used by SQLite to pass filenames
// to the Open method of a VFS.
//
// https://sqlite.org/c3ref/filename.html
type Filename struct {
	ctx   context.Context
	mod   api.Module
	zPath ptr_t
	flags OpenFlag
	stack [2]stk_t
}

// GetFilename is an internal API users should not call directly.
func ( context.Context,  api.Module,  ptr_t,  OpenFlag) *Filename {
	if  == 0 {
		return nil
	}
	return &Filename{
		ctx:   ,
		mod:   ,
		zPath: ,
		flags: ,
	}
}

// String returns this filename as a string.
func ( *Filename) () string {
	if  == nil || .zPath == 0 {
		return ""
	}
	return util.ReadString(.mod, .zPath, _MAX_PATHNAME)
}

// Database returns the name of the corresponding database file.
//
// https://sqlite.org/c3ref/filename_database.html
func ( *Filename) () string {
	return .path("sqlite3_filename_database")
}

// Journal returns the name of the corresponding rollback journal file.
//
// https://sqlite.org/c3ref/filename_database.html
func ( *Filename) () string {
	return .path("sqlite3_filename_journal")
}

// WAL returns the name of the corresponding WAL file.
//
// https://sqlite.org/c3ref/filename_database.html
func ( *Filename) () string {
	return .path("sqlite3_filename_wal")
}

func ( *Filename) ( string) string {
	if  == nil || .zPath == 0 {
		return ""
	}
	if .flags&(OPEN_MAIN_DB|OPEN_MAIN_JOURNAL|OPEN_WAL) == 0 {
		return ""
	}

	.stack[0] = stk_t(.zPath)
	 := .mod.ExportedFunction()
	if  := .CallWithStack(.ctx, .stack[:]);  != nil {
		panic()
	}
	return util.ReadString(.mod, ptr_t(.stack[0]), _MAX_PATHNAME)
}

// DatabaseFile returns the main database [File] corresponding to a journal.
//
// https://sqlite.org/c3ref/database_file_object.html
func ( *Filename) () File {
	if  == nil || .zPath == 0 {
		return nil
	}
	if .flags&(OPEN_MAIN_DB|OPEN_MAIN_JOURNAL|OPEN_WAL) == 0 {
		return nil
	}

	.stack[0] = stk_t(.zPath)
	 := .mod.ExportedFunction("sqlite3_database_file_object")
	if  := .CallWithStack(.ctx, .stack[:]);  != nil {
		panic()
	}
	,  := vfsFileGet(.ctx, .mod, ptr_t(.stack[0])).(File)
	return 
}

// URIParameter returns the value of a URI parameter.
//
// https://sqlite.org/c3ref/uri_boolean.html
func ( *Filename) ( string) string {
	if  == nil || .zPath == 0 {
		return ""
	}

	 := .mod.ExportedFunction("sqlite3_uri_key")
	.stack[0] = stk_t(.zPath)
	.stack[1] = stk_t(0)
	if  := .CallWithStack(.ctx, .stack[:]);  != nil {
		panic()
	}

	 := ptr_t(.stack[0])
	if  == 0 {
		return ""
	}

	// Parse the format from:
	// https://github.com/sqlite/sqlite/blob/41fda52/src/pager.c#L4821-L4864
	// This avoids having to alloc/free the key just to find a value.
	for {
		 := util.ReadString(.mod, , _MAX_NAME)
		if  == "" {
			return ""
		}
		 += ptr_t(len()) + 1

		 := util.ReadString(.mod, , _MAX_NAME)
		if  ==  {
			return 
		}
		 += ptr_t(len()) + 1
	}
}

// URIParameters obtains values for URI parameters.
//
// https://sqlite.org/c3ref/uri_boolean.html
func ( *Filename) () url.Values {
	if  == nil || .zPath == 0 {
		return nil
	}

	 := .mod.ExportedFunction("sqlite3_uri_key")
	.stack[0] = stk_t(.zPath)
	.stack[1] = stk_t(0)
	if  := .CallWithStack(.ctx, .stack[:]);  != nil {
		panic()
	}

	 := ptr_t(.stack[0])
	if  == 0 {
		return nil
	}

	var  url.Values

	// Parse the format from:
	// https://github.com/sqlite/sqlite/blob/41fda52/src/pager.c#L4821-L4864
	// This is the only way to support multiple valued keys.
	for {
		 := util.ReadString(.mod, , _MAX_NAME)
		if  == "" {
			return 
		}
		 += ptr_t(len()) + 1

		 := util.ReadString(.mod, , _MAX_NAME)
		if  == nil {
			 = url.Values{}
		}
		.Add(, )
		 += ptr_t(len()) + 1
	}
}