package org.whoneedhelp.mobile;

final class LaunchUrlResolver {
    private LaunchUrlResolver() {}

    static String initialUrl(
        TrustedOrigin trustedOrigin,
        String deepLinkUrl,
        String debugInitialUrl,
        boolean debugBuild
    ) {
        String candidate = incomingUrl(
            trustedOrigin,
            deepLinkUrl,
            debugInitialUrl,
            debugBuild
        );

        return candidate == null ? trustedOrigin.startUrl() : candidate;
    }

    static String incomingUrl(
        TrustedOrigin trustedOrigin,
        String deepLinkUrl,
        String debugInitialUrl,
        boolean debugBuild
    ) {
        if (deepLinkUrl != null && trustedOrigin.matches(deepLinkUrl)) {
            return deepLinkUrl;
        }

        if (
            debugBuild &&
            debugInitialUrl != null &&
            trustedOrigin.matches(debugInitialUrl)
        ) {
            return debugInitialUrl;
        }

        return null;
    }
}
