import { __assign } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { useMemo } from 'react';
import { asapScheduler, merge } from 'rxjs';
import { filter, observeOn } from 'rxjs/operators';
import FormContext, { useFormContext } from '../context';
import { isFieldSetModel } from '../models';
import { useModelFromContext } from './use-model';
export function FieldSetValue(_a) {
    var name = _a.name, modelProps = _a.model, children = _a.children;
    var ctx = useFormContext();
    var model = useModelFromContext(ctx, name, modelProps, isFieldSetModel);
    var childContext = useMemo(function () { return (__assign(__assign({}, ctx), { parent: model })); }, [ctx, model]);
    if (model) {
        return (_jsx(FormContext.Provider, __assign({ value: childContext }, { children: children }), model.id));
    }
    return null;
}
export function getFieldSetChildChangeObservable(fieldSet, name) {
    var $ = merge(fieldSet.childRegister$, fieldSet.childRemove$).pipe(observeOn(asapScheduler), filter(function (change) { return change === name; }));
    return $;
}
