package urls

import (
	"context"
	"errors"
	"fmt"
	"net/url"
	"path"

	"source.toby3d.me/toby3d/home/internal/domain"
	"source.toby3d.me/toby3d/home/internal/site"
)

type Namespace struct {
	siter site.UseCase
}

var ErrAbsURL error = errors.New("unsupported input type for AbsURL")

func New(siter site.UseCase) *Namespace {
	return &Namespace{
		siter: siter,
	}
}

func (ns *Namespace) AbsURL(p string) (string, error) {
	site, err := ns.siter.Do(context.Background(), domain.LanguageUnd)
	if err != nil {
		return "", fmt.Errorf("cannot fetch site root for AbsURL processing: %w", err)
	}

	return site.BaseURL.JoinPath(p).String(), nil
}

func (ns *Namespace) RelURL(p string) string {
	return path.Clean("/" + p)
}

func (ns *Namespace) Parse(rawUrl string) (*url.URL, error) {
	return url.Parse(rawUrl)
}
