/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
import { REACTIVE_NODE } from '../signals';
import { assertDefined, assertEqual } from '../util/assert';
import { markViewDirty } from './instructions/mark_view_dirty';
let currentConsumer = null;
export function setLViewForConsumer(node, lView) {
    (typeof ngDevMode === 'undefined' || ngDevMode) &&
        assertEqual(node.lView, null, 'Consumer already associated with a view.');
    node.lView = lView;
}
/**
 * Create a new template consumer pointing at the specified LView.
 * Sometimes, a previously created consumer may be reused, in order to save on allocations. In that
 * case, the LView will be updated.
 */
export function getReactiveLViewConsumer(lView, slot) {
    return lView[slot] ?? getOrCreateCurrentLViewConsumer();
}
/**
 * Assigns the `currentTemplateContext` to its LView's `REACTIVE_CONSUMER` slot if there are tracked
 * producers.
 *
 * The presence of producers means that a signal was read while the consumer was the active
 * consumer.
 *
 * If no producers are present, we do not assign the current template context. This also means we
 * can just reuse the template context for the next LView.
 */
export function commitLViewConsumerIfHasProducers(lView, slot) {
    const consumer = getOrCreateCurrentLViewConsumer();
    if (!consumer.producerNode?.length) {
        return;
    }
    lView[slot] = currentConsumer;
    consumer.lView = lView;
    currentConsumer = createLViewConsumer();
}
const REACTIVE_LVIEW_CONSUMER_NODE = {
    ...REACTIVE_NODE,
    consumerIsAlwaysLive: true,
    consumerMarkedDirty: (node) => {
        (typeof ngDevMode === 'undefined' || ngDevMode) &&
            assertDefined(node.lView, 'Updating a signal during template or host binding execution is not allowed.');
        markViewDirty(node.lView);
    },
    lView: null,
};
function createLViewConsumer() {
    return Object.create(REACTIVE_LVIEW_CONSUMER_NODE);
}
function getOrCreateCurrentLViewConsumer() {
    currentConsumer ??= createLViewConsumer();
    return currentConsumer;
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 5648 characters]