function pickNonEmptyString(v) {
    if (v == null)
        return undefined;
    const s = String(v).trim();
    return s === '' ? undefined : s;
}
function tryPlainObjectFromRequestData(data) {
    if (data == null)
        return undefined;
    let obj = data;
    if (typeof data === 'function') {
        try {
            obj = data();
        }
        catch {
            return undefined;
        }
    }
    if (obj == null || typeof obj !== 'object' || Array.isArray(obj))
        return undefined;
    return obj;
}
/**
 * 与 `requestCubeOpenPlatform` 一致：`createOrUpdateRequestConfig.data` 先铺底，`getListRequestConfig.data` 覆盖同名键。
 */
function mergedDimensionHintsFromRequestConfigs(cube) {
    var _a, _b;
    if (!cube)
        return {};
    const fromCo = tryPlainObjectFromRequestData((_a = cube.createOrUpdateRequestConfig) === null || _a === void 0 ? void 0 : _a.data);
    const fromGl = tryPlainObjectFromRequestData((_b = cube.getListRequestConfig) === null || _b === void 0 ? void 0 : _b.data);
    return { ...(fromCo !== null && fromCo !== void 0 ? fromCo : {}), ...(fromGl !== null && fromGl !== void 0 ? fromGl : {}) };
}
/**
 * 为「源码场景、无魔方实例、直连魔方开放平台」的 getList / createOrUpdate 准备页面维度参数。
 * 不读取 `__lowcode_schema__` / `__PAGE_INFO__`（源码环境不存在）。
 *
 * 业务显式维度仅来自 **`getListRequestConfig.data` / `createOrUpdateRequestConfig.data`**（与请求体一致，`createOrUpdate` 先、`getList` 覆盖同名键）。
 *
 * **pageId**：`data.pageId` → `currentPageConfig.id` → `tableId`
 *
 * **appCode**：`data.appCode` → `siteInfo.appCode`
 *
 * **tenantCode / loginType / userName / userId**：`data.*` → `_GATEWAY_PF`（与历史一致）。
 * 未提供 **userId** 时见 {@link getCubePlatformUserId}（网关 `osBucNo` / `workerNo` 等）。
 */
export function getCubePlatformMeta(tableId, bizRequestConfig) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const w = window;
    const gw = w._GATEWAY_PF;
    const si = w.siteInfo;
    const bizD = mergedDimensionHintsFromRequestConfigs(bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.cubePlatform);
    const pageIdFromWindow = ((_a = w.currentPageConfig) === null || _a === void 0 ? void 0 : _a.id) != null ? String(w.currentPageConfig.id) : undefined;
    const pageId = (_c = (_b = pickNonEmptyString(bizD.pageId)) !== null && _b !== void 0 ? _b : pageIdFromWindow) !== null && _c !== void 0 ? _c : pickNonEmptyString(tableId);
    const appCodeFromWindow = (si === null || si === void 0 ? void 0 : si.appCode) != null ? String(si.appCode) : undefined;
    const appCode = (_d = pickNonEmptyString(bizD.appCode)) !== null && _d !== void 0 ? _d : appCodeFromWindow;
    if (pageId == null || appCode == null) {
        return null;
    }
    const tenantCode = (_f = (_e = pickNonEmptyString(bizD.tenantCode)) !== null && _e !== void 0 ? _e : gw === null || gw === void 0 ? void 0 : gw.tenantCode) !== null && _f !== void 0 ? _f : '';
    const loginType = (_g = pickNonEmptyString(bizD.loginType)) !== null && _g !== void 0 ? _g : gw === null || gw === void 0 ? void 0 : gw.loginType;
    const userName = (_h = pickNonEmptyString(bizD.userName)) !== null && _h !== void 0 ? _h : gw === null || gw === void 0 ? void 0 : gw.userName;
    const userId = pickNonEmptyString(bizD.userId);
    return {
        pageId: String(pageId),
        appCode: String(appCode),
        tenantCode,
        loginType,
        userName,
        ...(userId != null ? { userId } : {}),
    };
}
