import React, { createContext, useContext, useMemo } from 'react';
export const ModelContext = createContext(null);
export function ModelProvider({ children, ...models }) {
    const parentModels = useContext(ModelContext);
    // 使用 Object.assign 代替展开语法，避免 webpack 5 PureExpressionDependency 错误
    // 每次渲染时重新计算，确保 models 更新能正确反映
    const mergedModels = Object.assign({}, parentModels || {}, models);
    return React.createElement(ModelContext.Provider, { value: mergedModels }, children);
}
export function useModel(model) {
    const models = useContext(ModelContext);
    let ret;
    if (models) {
        if (typeof model === 'string') {
            ret = models[model];
        }
        else {
            ret = Object.values(models).find((m) => m instanceof model);
        }
    }
    const localModel = useMemo(() => {
        if (typeof model === 'function' && !ret) {
            return model();
        }
    }, []);
    return ret || localModel;
}
