/**
 * @module sync-innerspace-multiple-timelines.respec
 *
 * Verifies Complex Sync Scenarios using InnerSpace.
 */
import { respecfully, ifWe, iReckon } 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 { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
import { 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 { createTimelineRootTestHelper, getTestKeystoneServiceHelper } from '../test-helpers.mjs';
import { mut8Timeline } from '../timeline/timeline-api.mjs';
import { DEFAULT_INNER_SPACE_DATA_V1 } from '../witness/space/inner-space/inner-space-types.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 logalot = true;
const lc = `[sync-innerspace-multiple-timelines.respec]`;
await respecfully(sir, `Sync InnerSpaces (Multiple Timelines)`, async () => {
    let metaspace;
    let sourceSpace;
    let destSpace;
    await respecfully(sir, `Multiple Timelines (Push Source -> Dest)`, async () => {
        // 1. Setup Spaces
        metaspace = new Metaspace_Innerspace(undefined);
        await metaspace.initialize({
            getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
            getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
            getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
        });
        while (!metaspace.initialized) {
            await delay(10);
        }
        const defaultLocalUserSpace = await metaspace.getLocalUserSpace({ lock: false });
        await defaultLocalUserSpace.initialized;
        sourceSpace = new InnerSpace_V1({
            ...DEFAULT_INNER_SPACE_DATA_V1,
            name: 'source',
            uuid: 'source_uuid',
            description: 'source test space',
        });
        await sourceSpace.initialized;
        destSpace = new InnerSpace_V1({
            ...DEFAULT_INNER_SPACE_DATA_V1,
            name: 'dest',
            uuid: 'dest_uuid',
            description: 'dest test space',
        });
        await destSpace.initialized;
        // 2. Seed Source Data (Multiple Independent Timelines)
        console.log(`${lc} Creating data in Source...`);
        // Timeline A
        const rootA = await createTimelineRootTestHelper({
            ib: 'timeline_root_A',
            data: { type: 'root', label: 'A' },
            space: sourceSpace,
        });
        const childA = await mut8Timeline({
            timeline: rootA,
            mut8Opts: { dataToAddOrPatch: { type: 'child', label: 'A1' } },
            metaspace,
            space: sourceSpace,
        });
        const addrA = getIbGibAddr({ ibGib: childA });
        // Timeline B
        const rootB = await createTimelineRootTestHelper({
            ib: 'timeline_root_B',
            data: { type: 'root', label: 'B' },
            space: sourceSpace,
        });
        // B has deeper history
        const childB1 = await mut8Timeline({
            timeline: rootB,
            mut8Opts: { dataToAddOrPatch: { type: 'child', label: 'B1' } },
            metaspace,
            space: sourceSpace,
        });
        const childB2 = await mut8Timeline({
            timeline: childB1,
            mut8Opts: { dataToAddOrPatch: { type: 'child', label: 'B2' } },
            metaspace,
            space: sourceSpace,
        });
        const addrB = getIbGibAddr({ ibGib: childB2 });
        // 3. Setup Coordinators & Peer
        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. Run Sync (Push Both)
        console.log(`${lc} Running Sync...`);
        const resSync = await senderCoordinator.sync({
            peer: peer,
            localSpace: sourceSpace,
            metaspace: metaspace,
            domainIbGibs: [childA, childB2], // Sync both tips
            useSessionIdentity: false,
        });
        await resSync.done;
        // await delay(100); // Wait for flush?
        // 5. Verify Dest
        console.log(`${lc} Verifying Destination...`);
        await ifWe(sir, `verify timelines present`, async () => {
            // Verify A
            const getA = await getFromSpace({ space: destSpace, addr: addrA });
            iReckon(sir, getA.success).asTo('Timeline A present').isGonnaBeTrue();
            iReckon(sir, getA.ibGibs?.[0]?.data?.label).asTo('Label A match').isGonnaBe('A1');
            // Verify B
            const getB = await getFromSpace({ space: destSpace, addr: addrB });
            iReckon(sir, getB.success).asTo('Timeline B present').isGonnaBeTrue();
            iReckon(sir, getB.ibGibs?.[0]?.data?.label).asTo('Label B match').isGonnaBe('B2');
        });
    });
});
//# sourceMappingURL=sync-innerspace-multiple-timelines.respec.mjs.map