/**
 * @module sync-innerspace-constants.respec
 *
 * Verifies syncing "Constants" (ibGibs without timelines/TJPs).
 * Also verifies Smart Diff behavior (Constants shouldn't be re-sent).
 */
import { respecfully, iReckon, ifWe } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
const maam = `[${import.meta.url}]`, sir = maam;
import { clone, delay } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/index.mjs';
import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
import { putInSpace, getFromSpace } from '../witness/space/space-helper.mjs';
import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs';
import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs';
import { DEFAULT_INNER_SPACE_DATA_V1 } from '../witness/space/inner-space/inner-space-types.mjs';
import { getTestKeystoneServiceHelper } from '../test-helpers.mjs';
import { SyncPeerInnerspace_V1 } from './sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs';
import { SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1 } from './sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs';
const lc = `[sync-innerspace-constants.respec]`;
await respecfully(sir, `Sync Constants (No TJP)`, async () => {
    await ifWe(sir, `Verify Constants Sync`, async () => {
        // 1. Setup Spaces
        const metaspace = new Metaspace_Innerspace(undefined);
        await metaspace.initialize({
            getFnAlert: () => async ({ title, msg }) => { },
            getFnPrompt: () => async ({ title, msg }) => { return ''; },
            getFnPromptPassword: () => async (title, msg) => { return null; },
        });
        while (!metaspace.initialized) {
            await delay(10);
        }
        const sourceSpace = new InnerSpace_V1({
            ...DEFAULT_INNER_SPACE_DATA_V1,
            name: 'source',
            uuid: 'source_uuid',
            description: 'source test space',
        });
        await sourceSpace.initialized;
        const destSpace = new InnerSpace_V1({
            ...DEFAULT_INNER_SPACE_DATA_V1,
            name: 'dest',
            uuid: 'dest_uuid',
            description: 'dest test space',
        });
        await destSpace.initialized;
        // 2. Create Constant (C1) in Source
        // Factory_V1.constant returns Promise<IbGib_V1>, not { newIbGib: ... }
        const c1 = await Factory_V1.constant({
            parentPrimitiveIb: 'root',
            ib: 'constant_c1',
            data: { some: 'data' },
        });
        const addrC1 = getIbGibAddr({ ibGib: c1 });
        await putInSpace({ space: sourceSpace, ibGibs: [c1] });
        const checkDest1 = await getFromSpace({ space: destSpace, addr: addrC1 });
        iReckon(sir, checkDest1.success).asTo('Dest initially does not have C1').isGonnaBeFalse();
        // 3. Setup Coordinator
        const mockKeystone = await getTestKeystoneServiceHelper();
        const senderCoordinator = new SyncSagaCoordinator(mockKeystone);
        const receiverCoordinator = new SyncSagaCoordinator(mockKeystone);
        const peer = new SyncPeerInnerspace_V1(clone(SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1));
        await peer.initialized;
        await peer.initializeSender({
            senderSpace: sourceSpace, // "Client"
            receiverSpace: destSpace, // "Server"
            receiverCoordinator,
            receiverMetaspace: metaspace,
        });
        // 4. Sync C1 (First Pass)
        let resSync = await senderCoordinator.sync({
            peer,
            localSpace: sourceSpace,
            metaspace: metaspace,
            domainIbGibs: [c1],
            useSessionIdentity: false,
        });
        await resSync.done;
        iReckon(sir, true).asTo('Sync execution 1 completes').isGonnaBeTrue();
        // Verify C1 in Dest
        const getC1Dest = await getFromSpace({ space: destSpace, addr: addrC1 });
        iReckon(sir, getC1Dest.success && !!getC1Dest.ibGibs[0]).asTo('Dest has C1 after sync').isGonnaBeTrue();
        // // 5. Sync C1 (Second Pass - Idempotency / Smart Diff)
        // // Should NOT send payload again.
        // resSync = await senderCoordinator.sync({
        //     peer: peer,
        //     localSpace: sourceSpace,
        //     metaspace: metaspace,
        //     domainIbGibs: [c1],
        //     useSessionIdentity: false,
        // });
        // await resSync.done;
        // iReckon(sir, true).asTo('Sync execution 2 (idempotent) completes').isGonnaBeTrue();
        // // 6. Create "Constant with Dependency" (C2 -> C1)
        // const c2 = await Factory_V1.constant({
        //     ib: 'constant_c2',
        //     parentPrimitiveIb: 'root',
        //     rel8ns: {
        //         link: [addrC1], // C2 links to C1
        //     },
        // });
        // const addrC2 = getIbGibAddr({ ibGib: c2 });
        // await putInSpace({ space: sourceSpace, ibGibs: [c2] });
        // // Sync C2
        // resSync = await senderCoordinator.sync({
        //     peer: peer,
        //     localSpace: sourceSpace,
        //     metaspace: metaspace,
        //     domainIbGibs: [c2],
        //     useSessionIdentity: false,
        // });
        // await resSync.done;
        // iReckon(sir, true).asTo('Sync C2 completes').isGonnaBeTrue();
        // // Verify C2 in Dest
        // const getC2Dest = await getFromSpace({ space: destSpace, addr: addrC2 });
        // iReckon(sir, getC2Dest.success).asTo('Dest has C2').isGonnaBeTrue();
    });
});
//# sourceMappingURL=sync-innerspace-constants.respec.mjs.map