File size: 5,266 Bytes
aeb61fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Runtime configuration for ColorRM
// This file is used to configure API endpoints when running in bundled mode

export const Config = {
    // Backend URLs
    BACKENDS: {
        hf: 'https://jaimodiji-my-multiplayer-app.hf.space',
        cloudflare: 'https://multiplayer-template.bossemail.workers.dev'
    },

    // Detect if running in Capacitor (check multiple indicators)
    isCapacitor() {
        if (typeof window === 'undefined') return false;

        // Check for Capacitor object
        if (window.Capacitor) return true;

        // Check for capacitor:// protocol (bundled mode)
        if (window.location.protocol === 'capacitor:') return true;

        // Check for file:// with capacitor in path
        if (window.location.protocol === 'file:' &&
            window.location.href.includes('capacitor')) return true;

        // Check for http:// on localhost with empty or file-like path (Android WebView bundled)
        if (window.location.protocol === 'http:' &&
            window.location.host === 'localhost' &&
            window.location.pathname.startsWith('/')) {
            // This could be bundled Capacitor or local dev - check for Capacitor later
            return false;
        }

        // Check for https:// on localhost (Android secure WebView)
        if (window.location.protocol === 'https:' &&
            window.location.host.includes('localhost')) return true;

        return false;
    },

    // Check if running in remote mode (WebView pointing to server)
    isRemoteMode() {
        if (typeof window === 'undefined') return false;
        const host = window.location.host || '';

        // If host is empty or file-based, not remote mode
        if (!host || host === '') return false;

        return host.includes('hf.space') ||
               host.includes('workers.dev') ||
               host.includes('duckdns.org') ||
               host.includes('192.168.');
        // Note: removed 'localhost' - it should trigger bundled mode detection
    },

    // Force bundled mode check - called after Capacitor is definitely loaded
    isBundledMode() {
        // If Capacitor is present and we're not pointing to a remote server, we're bundled
        if (window.Capacitor && !this.isRemoteMode()) {
            return true;
        }
        return false;
    },

    // Get the API base URL
    // In bundled mode, this returns the full backend URL
    // In remote/web mode, this returns empty string (relative URLs work)
    getApiBase() {
        // Remote mode: relative URLs work
        if (this.isRemoteMode()) {
            return '';
        }

        // Bundled Capacitor mode: need absolute URL to backend
        if (this.isBundledMode() || this.isCapacitor()) {
            const defaultBackend = 'cloudflare'; // REPLACED_BY_BUILD_SCRIPT
            const preferredBackend = localStorage.getItem('color_rm_backend') || defaultBackend;
            const base = this.BACKENDS[preferredBackend] || this.BACKENDS[defaultBackend];
            console.log('[Config] Bundled mode - using backend:', base);
            return base;
        }

        // Web browser: relative URLs work
        return '';
    },

    // Helper to make API URL
    apiUrl(path) {
        const base = this.getApiBase();
        // Ensure path starts with /
        const normalizedPath = path.startsWith('/') ? path : '/' + path;
        return base + normalizedPath;
    },

    // Helper to make WebSocket URL (for Yjs sync, etc.)
    // Handles Capacitor bundled mode where window.location.host is not the server
    wsUrl(path) {
        const base = this.getApiBase();
        // Ensure path starts with /
        const normalizedPath = path.startsWith('/') ? path : '/' + path;

        if (base) {
            // Bundled mode - convert https:// to wss:// or http:// to ws://
            const wsBase = base.replace(/^https:/, 'wss:').replace(/^http:/, 'ws:');
            return wsBase + normalizedPath;
        } else {
            // Remote/web mode - use current host
            const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            return `${protocol}//${window.location.host}${normalizedPath}`;
        }
    },

    // Set preferred backend (for bundled mode)
    setBackend(backend) {
        if (this.BACKENDS[backend]) {
            localStorage.setItem('color_rm_backend', backend);
            console.log(`[Config] Backend set to: ${backend} (${this.BACKENDS[backend]})`);
            return true;
        }
        return false;
    },

    // Debug info
    getDebugInfo() {
        const defaultBackend = 'cloudflare'; // REPLACED_BY_BUILD_SCRIPT
        return {
            isCapacitor: this.isCapacitor(),
            isBundledMode: this.isBundledMode(),
            isRemoteMode: this.isRemoteMode(),
            protocol: window.location.protocol,
            host: window.location.host,
            href: window.location.href,
            apiBase: this.getApiBase(),
            preferredBackend: localStorage.getItem('color_rm_backend') || defaultBackend
        };
    }
};

// Make available globally immediately
if (typeof window !== 'undefined') {
    window.Config = Config;
    console.log('[Config] Initialized:', Config.getDebugInfo());
}