BareGit
(function exportCardFormLogic(root)
{
    /** Return whether a URL is an absolute HTTP or HTTPS image source. */
    function validRemoteImageUrl(value)
    {
        try
        {
            const url = new URL(value);
            return url.protocol == "http:" || url.protocol == "https:";
        }
        catch(error)
        {
            return false;
        }
    }

    /** Return whether a changed resulting foil card needs a thumbnail. */
    function foilThumbnailRequired(options)
    {
        const rendering_changed = options.mode == "create" ||
            options.front_action == "replace" ||
            options.foil_action != "keep";
        let resulting_foil = options.has_current_foil;
        if(options.foil_action == "remove")
        {
            resulting_foil = false;
        }
        else if(options.foil_action == "replace")
        {
            resulting_foil = options.has_replacement_foil;
        }
        return rendering_changed && resulting_foil;
    }

    /** Return enabled and required image inputs for the current form state. */
    function sourceInputState(options)
    {
        const uses_files = options.source_mode == "files";
        const needs_front = options.mode == "create" ||
            options.front_action == "replace";
        const replaces_foil = options.mode == "create" ||
            options.foil_action == "replace";
        return {
            front_file_enabled: uses_files && needs_front,
            foil_file_enabled: uses_files && replaces_foil,
            front_url_enabled: !uses_files && needs_front,
            foil_url_enabled: !uses_files && replaces_foil,
            front_required: needs_front,
            foil_required: options.mode == "edit" && replaces_foil,
        };
    }

    /** Return whether controls for one game belong to the selection. */
    function gameControlEnabled(selected_game, control_game)
    {
        return selected_game == control_game;
    }

    /** Return whether a series choice belongs to the selected game. */
    function seriesChoiceEnabled(selected_game, series_game)
    {
        return selected_game != "" && selected_game == series_game;
    }

    const api = {
        validRemoteImageUrl,
        foilThumbnailRequired,
        sourceInputState,
        gameControlEnabled,
        seriesChoiceEnabled,
    };
    if(typeof module != "undefined" && module.exports != null)
    {
        module.exports = api;
    }
    else
    {
        root.cardFormLogic = api;
    }
})(globalThis);