import { update_path } from './state.js';
import { debug } from './util.js';
import { get_access_list } from '../lib/util.js';
export function run_schedule(state, marker) {
    debug('[vm] running tasks up to marker:', marker);
    const tasks = get_tasks(state);
    for (const task of tasks) {
        const [ts, action, paths] = task;
        const stamp = state.start + ts;
        const prev = state.updated;
        if (prev <= stamp && stamp <= marker) {
            debug('[vm] running task:', task);
            run_task(action, paths, state);
            if (state.result !== null)
                return;
            state.tasks.shift();
        }
    }
}
function run_task(action, pathexp, state) {
    const paths = state.paths.map(e => e[0]);
    const expr = get_access_list(pathexp, paths);
    for (const path of expr.wlist) {
        try {
            update_path(action, path, state);
            if (state.result !== null)
                return;
        }
        catch (err) {
            debug('[vm] task failed to execute:' + String(err));
        }
    }
}
function get_tasks(state) {
    const { start, updated } = state;
    return state.tasks.filter(e => e[0] + start > updated);
}
