var _a, _b;
/**
 * get the "right" `postMessage`, fallback to a dev log if none is available
 *
 * n.b. assigning `<whatever>.mobileApp?.postMessage` to a variable won't work
 * because it loses its "this binding" when passed as a reference
 *
 * @see https://stackoverflow.com/questions/59158951/java-bridge-method-cant-be-invoked-on-a-non-injected-object
 * @see https://github.com/react-native-webview/react-native-webview/issues/323#issuecomment-511824940
 * @see https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String)
 */
const postMessage = (message) => {
    var _a, _b, _c, _d, _e, _f;
    if ((_b = (_a = window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers.mobileApp) === null || _b === void 0 ? void 0 : _b.postMessage) {
        (_d = (_c = window.webkit) === null || _c === void 0 ? void 0 : _c.messageHandlers.mobileApp) === null || _d === void 0 ? void 0 : _d.postMessage(message);
        return;
    }
    if ((_e = window.mobileApp) === null || _e === void 0 ? void 0 : _e.postMessage) {
        (_f = window.mobileApp) === null || _f === void 0 ? void 0 : _f.postMessage(message);
        return;
    }
    if (process.env.NODE_ENV === 'development') {
        console.log(`posting to mobile app with message:\n${JSON.stringify(JSON.parse(message), null, 2)}`);
    }
};
/**
 * the mobile app loads `tactical-map-client` as a WebView with search params:
 *
 * - `&disableMb=true` disables rendering of the message-broker-client
 * 		app-portal in app-shell
 * - `&disableLayout=true` disables rendering web layout (header, footer, nav)
 * - `&gameId=<game id>` the game id
 * - `&userId=<user id>` the current user id
 *
 * @see https://wikia-inc.atlassian.net/wiki/spaces/MOB/pages/1548877853/Access+tactical+maps+in+the+app
 * @see https://github.com/DnDBeyond/ddb-app-shell/pull/67
 */
const urlSearchParams = new URLSearchParams(location.search);
export const isMobileApp = urlSearchParams.get('disableMb') === 'true' &&
    urlSearchParams.get('disableLayout') === 'true' &&
    urlSearchParams.has('userId'); // might not need this?
// TODO: throw if `userId` is missing? decode it from JWT?
window.mobileApp || (window.mobileApp = {});
window.mobileApp.subscribers || (window.mobileApp.subscribers = new Set());
window.mobileApp.gameLogCallback ||
    (window.mobileApp.gameLogCallback = (message) => {
        var _a, _b;
        return (_b = (_a = window.mobileApp) === null || _a === void 0 ? void 0 : _a.subscribers) === null || _b === void 0 ? void 0 : _b.forEach((subscription) => subscription(message));
    });
export const messageBrokerFacade = {
    gameId: (_a = urlSearchParams.get('gameId')) !== null && _a !== void 0 ? _a : '0',
    userId: (_b = urlSearchParams.get('userId')) !== null && _b !== void 0 ? _b : '0',
    source: '',
    connectUrl: '',
    getMessagesUrl: '',
    fetchHistory: () => Promise.resolve(),
    subscribe: (callback) => {
        var _a, _b;
        (_b = (_a = window.mobileApp) === null || _a === void 0 ? void 0 : _a.subscribers) === null || _b === void 0 ? void 0 : _b.add(callback);
        return () => { var _a, _b; return (_b = (_a = window.mobileApp) === null || _a === void 0 ? void 0 : _a.subscribers) === null || _b === void 0 ? void 0 : _b.delete(callback); };
    },
    dispatch: (partial) => 
    /**
     * in `GameLogContextProvider` we have an `isMessage` type predicate that
     * "duck types" a message as being anything that includes all of these keys:
     * `entityId`, `entityType`, `persist`, `messageScope`, `messageTarget`, and
     * `data`, so I think it's important we preserve that shape here. The
     * additional `type` key here is (I belive) just for the mobile apps to
     * filter messages against
     *
     * @see https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
     * @see packages/game-log-components/src/components/GameLogContextProvider/GameLogContextProvider.tsx
     */
    postMessage(JSON.stringify(Object.assign({ type: 'gameLog' }, partial))),
};
//# sourceMappingURL=mobileApp.js.map