const API_URL = 'https://inventrack-api.henryanto2012.workers.dev/api/rpc'; window.google = { script: { run: new Proxy({}, { get(_, prop) { if (prop === 'withSuccessHandler') return (onSuccess) => createRunner(onSuccess, null); if (prop === 'withFailureHandler') return (onFailure) => createRunner(null, onFailure); return (...args) => callRpc(prop, args, null, null); } }) } }; function createRunner(onSuccess, onFailure) { return new Proxy({}, { get(_, prop) { if (prop === 'withSuccessHandler') return (cb) => createRunner(cb, onFailure); if (prop === 'withFailureHandler') return (cb) => createRunner(onSuccess, cb); return (...args) => callRpc(prop, args, onSuccess, onFailure); } }); } async function callRpc(action, args, onSuccess, onFailure) { try { const noAuthActions = ['login', 'loginAndGetState', 'setupDatabase']; const token = noAuthActions.includes(action) ? null : args[0]; const cleanArgs = noAuthActions.includes(action) ? args : args.slice(1); const res = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action, token, args: cleanArgs }), }); const data = await res.json(); if (!res.ok || data.ok === false) { throw new Error(data.error || 'Gagal terhubung ke server.'); } if (onSuccess) onSuccess(data); } catch (err) { if (onFailure) onFailure(err); else console.error(err); } }