const URLUtils = {

    parseEntityPath(basePath = 'anime') {
        const path = window.location.pathname;
        const parts = path.split("/").filter(Boolean);

        if (parts[0] !== basePath) {
            return null;
        }

        if (parts.length === 3) {

            return {
                extensionName: parts[1],
                entityId: parts[2],
                slug: parts[2]
            };
        } else if (parts.length === 2) {

            return {
                extensionName: null,
                entityId: parts[1],
                slug: null
            };
        }

        return null;
    },

    buildWatchUrl(animeId, episode, extensionName = null) {
        const base = `/watch/${animeId}/${episode}`;
        return extensionName ? `${base}?${extensionName}` : base;
    },

    buildReadUrl(bookId, chapterId, provider, extensionName = null) {
        const c = encodeURIComponent(chapterId);
        const p = encodeURIComponent(provider);
        const extension = extensionName ? `?source=${extensionName}` : "?source=anilist";
        return `/read/${p}/${c}/${bookId}${extension}`;
    },

    getQueryParams() {
        return new URLSearchParams(window.location.search);
    },

    getQueryParam(key) {
        return this.getQueryParams().get(key);
    }
};

window.URLUtils = URLUtils;