"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useViewsConfigs = useViewsConfigs;
const React = __importStar(require("react"));
const get_cube_platform_meta_1 = require("../../../cn-ai-table/cube-platform/open-platform/get-cube-platform-meta");
const services_1 = require("../../../cn-ai-table/view/services");
/** 无 tableId 或接口异常时的占位，与 `ViewsData` 空结构一致 */
const EMPTY_VIEWS = { views: {}, viewOrder: [] };
/**
 * 视图配置数据源（低码与源码同一套分支）：
 * - 配 `viewConfig.queryViewsUrl`：`queryTableViews`
 * - 否则：`fetchViewsFromCube`（与 {@link getCubePlatformMeta} 可解析时一致，含低码侧传入 `cubePlatform`）
 *
 * @returns effectiveViewsConfigs — 交给 `DataModel.setAllViews` 的静态包；未就绪时为 `null`
 * @returns isLoadingViews — 已配业务 query URL 且首次请求未回时为 true，用于全屏 Loading
 */
function useViewsConfigs(tableId, bizRequestConfig) {
    var _a;
    const bizRequestConfigRef = React.useRef(bizRequestConfig);
    bizRequestConfigRef.current = bizRequestConfig;
    const [bizUrlViews, setBizUrlViews] = React.useState(null);
    const [directCubeViews, setDirectCubeViews] = React.useState(null);
    const queryViewsUrl = (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.viewConfig) === null || _a === void 0 ? void 0 : _a.queryViewsUrl;
    const effectiveViewsConfigs = queryViewsUrl ? bizUrlViews : directCubeViews;
    React.useEffect(() => {
        if (!queryViewsUrl) {
            return;
        }
        if (!tableId) {
            setBizUrlViews({ type: 'static', data: EMPTY_VIEWS });
            return;
        }
        let cancelled = false;
        (async () => {
            const data = await (0, services_1.queryTableViews)(tableId, bizRequestConfigRef.current);
            if (!cancelled) {
                setBizUrlViews({ type: 'static', data });
            }
        })();
        return () => {
            cancelled = true;
        };
    }, [queryViewsUrl, tableId]);
    React.useEffect(() => {
        if (queryViewsUrl) {
            return;
        }
        if (!tableId) {
            setDirectCubeViews({ type: 'static', data: EMPTY_VIEWS });
            return;
        }
        const biz = bizRequestConfigRef.current;
        if (!(0, get_cube_platform_meta_1.getCubePlatformMeta)(tableId, biz)) {
            setDirectCubeViews({ type: 'static', data: EMPTY_VIEWS });
            return;
        }
        let cancelled = false;
        (async () => {
            const data = await (0, services_1.fetchViewsFromCube)(tableId, bizRequestConfigRef.current);
            if (!cancelled) {
                setDirectCubeViews({ type: 'static', data: data.views });
            }
        })();
        return () => {
            cancelled = true;
        };
        /** 须依赖完整 `bizRequestConfig`：`JSON.stringify(cubePlatform)` 会丢掉 `service` 等函数 */
    }, [queryViewsUrl, tableId, bizRequestConfig]);
    const isLoadingViews = Boolean(queryViewsUrl && bizUrlViews === null);
    return {
        effectiveViewsConfigs,
        isLoadingViews,
    };
}
