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 {
	wrp   *sqlite3_wrap.Wrapper
	zPath ptr_t
	flags OpenFlag
}

// GetFilename is an internal API users should not call directly.
func ( *sqlite3_wrap.Wrapper,  ptr_t,  OpenFlag) *Filename {
	if  == 0 {
		return nil
	}
	return &Filename{
		wrp:   ,
		zPath: ,
		flags: ,
	}
}

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

// Database returns the name of the corresponding database file.
//
// https://sqlite.org/c3ref/filename_database.html
func ( *Filename) () string {
	if  == nil || .zPath == 0 {
		return ""
	}
	return .path(.wrp.Xsqlite3_filename_database)
}

// Journal returns the name of the corresponding rollback journal file.
//
// https://sqlite.org/c3ref/filename_database.html
func ( *Filename) () string {
	if  == nil || .zPath == 0 {
		return ""
	}
	return .path(.wrp.Xsqlite3_filename_journal)
}

// WAL returns the name of the corresponding WAL file.
//
// https://sqlite.org/c3ref/filename_database.html
func ( *Filename) () string {
	if  == nil || .zPath == 0 {
		return ""
	}
	return .path(.wrp.Xsqlite3_filename_wal)
}

func ( *Filename) ( func(int32) int32) string {
	if .flags&(OPEN_MAIN_DB|OPEN_MAIN_JOURNAL|OPEN_WAL) == 0 {
		return ""
	}
	 := ptr_t((int32(.zPath)))
	return .wrp.ReadString(, _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
	}

	 := ptr_t(.wrp.Xsqlite3_database_file_object(int32(.zPath)))
	,  := vfsFileGet(.wrp, ptr_t()).(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 ""
	}

	 := ptr_t(.wrp.Xsqlite3_uri_key(int32(.zPath), 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.
	 := .wrp.Memory
	for {
		 := .ReadString(, _MAX_NAME)
		if  == "" {
			return ""
		}
		 += ptr_t(len()) + 1

		 := .ReadString(, _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
	}

	 := ptr_t(.wrp.Xsqlite3_uri_key(int32(.zPath), 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.
	 := .wrp.Memory
	for {
		 := .ReadString(, _MAX_NAME)
		if  == "" {
			return 
		}
		 += ptr_t(len()) + 1

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