export function exec(params, store) {
    const [threshold, ...members] = params;
    const thold = Number(threshold);
    return (witness) => {
        const { action, path, sigs } = witness;
        for (const proof of sigs) {
            const pub = proof.slice(0, 64);
            if (!members.includes(pub)) {
                throw 'pubkey not a member of the program';
            }
            const count = record_entry(store, `${path}/${action}`, pub);
            if (count >= thold)
                return true;
        }
        return false;
    };
}
function record_entry(store, label, pubkey) {
    const entries = JSON.parse(store[1]);
    const idx = entries.findIndex(e => e[0] === label);
    let arr;
    if (idx === -1) {
        arr = new Array(pubkey);
    }
    else {
        arr = entries[idx][1];
        if (arr.includes(pubkey)) {
            throw `record already exists: ${label} => ${pubkey}`;
        }
        else {
            arr.push(pubkey);
        }
    }
    if (idx === -1) {
        entries.push([label, arr]);
    }
    else {
        entries[idx][1] = arr;
    }
    store[1] = JSON.stringify(entries);
    return arr.length;
}
