package app

import (
	
	
)

// ResourceResolver is an interface that defines the method to resolve
// resources from /web/ path to its full URL or file location.
type ResourceResolver interface {
	// Resolve takes a resource path and returns its resolved URL or file path.
	Resolve(string) string
}

// LocalDir returns a ResourceResolver for local resources. It resolves paths
// starting with /web/ to their full file path based on the specified local directory.
// This resolver is suitable for handling resources stored in the local filesystem.
func ( string) ResourceResolver {
	 = strings.TrimRight(, "/")
	return localResourceResolver{
		Handler:   http.FileServer(http.Dir()),
		directory: ,
	}
}

type localResourceResolver struct {
	http.Handler
	directory string
}

func ( localResourceResolver) ( string) string {
	if  == "/" ||  == "" {
		return "/"
	}
	if remoteLocation() || !webLocation() {
		return 
	}
	return .directory + "/" + strings.Trim(, "/")
}

// RemoteBucket returns a ResourceResolver for remote resources. It resolves
// paths starting with /web/ to their full URL based on the specified remote URL,
// such as a cloud storage bucket. This resolver is ideal for resources hosted
// remotely.
func ( string) ResourceResolver {
	return remoteResourceResolver{
		url: strings.Trim(, "/"),
	}
}

type remoteResourceResolver struct {
	url string
}

func ( remoteResourceResolver) ( string) string {
	if  == "/" ||  == "" {
		return "/"
	}
	if remoteLocation() || !webLocation() {
		return 
	}
	return .url + "/" + strings.Trim(, "/")
}

// PrefixedLocation returns a ResourceResolver that resolves resources with
// a specified prefix. This resolver prepends the given prefix to resource paths,
// which is particularly useful when serving resources from a specific directory
// or URL path.
//
// The prefix is added to the beginning of the resource path, effectively
// modifying the path from which the resources are served.
//
// For example, if the prefix is "/assets", a resource path like "/web/main.css"
// will be resolved as "/assets/web/main.css".
func ( string) ResourceResolver {
	return prefixedResourceResolver{
		prefix: ,
	}
}

type prefixedResourceResolver struct {
	localResourceResolver
	prefix string
}

func ( prefixedResourceResolver) ( string) string {
	if remoteLocation() {
		return 
	}
	 = .localResourceResolver.Resolve()
	if  == "/" {
		return strings.TrimRight(.prefix, "/")
	}
	return strings.TrimRight(.prefix, "/") + 
}

// GitHubPages returns a ResourceResolver tailored for serving resources
// from a GitHub Pages site. It creates a resolver with a prefix matching
// the given repository name. This is particularly useful when hosting
// a go-app project on GitHub Pages, where resources need to be served
// from a repository-specific subpath.
//
// For example, if the repository name is "myapp", the resources will be
// served from paths starting with "/myapp/web/".
func ( string) ResourceResolver {
	return PrefixedLocation("/" + strings.Trim(, "/"))
}

func clientResourceResolver( string) func(string) string {
	return func( string) string {
		if remoteLocation() || !webLocation() {
			return 
		}
		 = strings.Trim(, "/")
		return  + "/" + strings.TrimPrefix(, "web/")
	}
}

func resolveOGResource( string,  string) string {
	if remoteLocation() {
		return 
	}
	return "https://" +  + strings.TrimRight("/"+strings.Trim(, "/"), "/")
}

func remoteLocation( string) bool {
	return strings.HasPrefix(, "https://") ||
		strings.HasPrefix(, "http://")
}

func webLocation( string) bool {
	return strings.HasPrefix(, "/web/") ||
		 == "/web" ||
		strings.HasPrefix(, "web/") ||
		 == "web"
}

// ProxyResource is a proxy descriptor that maps a given resource to an URL
// path.
type ProxyResource struct {
	// The URL path from where a static resource is accessible.
	Path string

	// The path of the static resource that is proxied. It must start with
	// "/web/".
	ResourcePath string
}