/**
 * @module agent-helpers
 *
 * Helper functions designed to simplify common ibGib operations for agentic usage.
 * These abstract away the low-level Factory_V1 details and provide clear, semantic
 * actions for creating and modifying data.
 */
import { extractErrorMsg, getUUID } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { GLOBAL_LOG_A_LOT } from './core-constants.mjs';
import { KeystoneService_V1 } from './keystone/keystone-service-v1.mjs';
import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
import { persistTransformResult, registerNewIbGib } from './witness/space/space-helper.mjs';
const logalot = GLOBAL_LOG_A_LOT;
/**
 * Creates a "Stone" ibGib - an immutable, primitive-like data record with no history/timeline.
 * Useful for static data, configurations, or standalone records.
 */
export async function createStoneHelper({ ib, data, rel8ns, uuid, noTimestamp, }) {
    const lc = `[${createStoneHelper.name}]`;
    try {
        if (logalot) {
            console.log(`${lc} starting... (I: e4b15ee74de22ec9a917c578adc83425)`);
        }
        if (!ib) {
            throw new Error(`ib required (E: 3d5df8fd5b880e0e0dcedd37dbc3f825)`);
        }
        // the parent primitive ib is almost always the atom of the ib.
        const atom = ib.split(' ').at(0);
        if (uuid) {
            if (!data) {
                data = {};
            }
            data.uuid = await getUUID();
        }
        const stone = await Factory_V1.stone({
            parentPrimitiveIb: atom,
            ib,
            data,
            rel8ns,
            uuid,
        });
        return stone;
    }
    catch (error) {
        console.error(`${lc} ${extractErrorMsg(error)}`);
        throw error;
    }
    finally {
        if (logalot) {
            console.log(`${lc} complete.`);
        }
    }
}
/**
 * Creates a new "Living" Timeline Root.
 * This is the starting point for any mutable data structure (a file, a user profile, a post).
 */
export async function createTimelineRootHelper({ ib, data, space, }) {
    const lc = `[${createTimelineRootHelper.name}]`;
    try {
        if (logalot) {
            console.log(`${lc} starting... (I: 8f50a8cf0268742048f7c188feb5a825)`);
        }
        // const resTimeline = await createTimeline({
        //     ib,
        //     data,
        //     rel8ns: undefined,
        //     metaspace, space
        // });
        // const timeline = resTimeline.newIbGib;
        // return timeline;
        // Determine the parent to fork based on the provided options.
        const atom = ib.split(' ').at(0);
        const parentIbGib = Factory_V1.primitive({ ib: atom });
        const resNew = await Factory_V1.firstGen({
            parentIbGib,
            ib,
            data,
            rel8ns: undefined,
            // these options are what make this a "timeline"
            dna: true,
            nCounter: true,
            tjp: { timestamp: true, uuid: true },
        });
        // Persist the new timeline and its DNA to the designated space.
        await persistTransformResult({ resTransform: resNew, space });
        await registerNewIbGib({
            ibGib: resNew.newIbGib,
            space,
            /**
             * We don't need to broadcast here because it's a new, independent
             * timeline, so there are no listeners yet.
             */
            fnBroadcast: undefined,
        });
        // We don't need to lock here because it's a new, independent timeline,
        // so there are no concurrency concerns yet.
        return resNew.newIbGib;
    }
    catch (error) {
        console.error(`${lc} ${extractErrorMsg(error)}`);
        throw error;
    }
    finally {
        if (logalot) {
            console.log(`${lc} complete.`);
        }
    }
}
/**
 * Creates a valid, mock Keystone Service for testing purposes.
 * This mocks the identity and signing logic needed for Sync operations.
 */
export async function getTestKeystoneServiceHelper() {
    class MockKeystoneService extends KeystoneService_V1 {
        async getIdentity() {
            // Return a dummy identity
            // We use the Factory directly here to just get *something* that looks like an ibgib
            const res = await Factory_V1.firstGen({
                parentIbGib: Factory_V1.primitive({ ib: 'identity' }),
                ib: 'identity',
                data: { uuid: await getUUID() },
                dna: true,
            });
            return res.newIbGib;
        }
        // Implement other methods as no-ops or simple mocks
        async sign(args) { return args.latestKeystone; /* minimal mock */ }
        async verify(args) { return []; /* no errors */ }
    }
    return new MockKeystoneService();
}
//# sourceMappingURL=agent-helpers.mjs.map