import { getUUID, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { ifWe, iReckon, respecfully, } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
const maam = `[${import.meta.url}]`, sir = maam;
import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs';
import { mut8 } from '@ibgib/ts-gib/dist/V1/transforms/mut8.mjs';
import { rel8 } from '@ibgib/ts-gib/dist/V1/transforms/rel8.mjs';
import { fork } from '@ibgib/ts-gib/dist/V1/transforms/fork.mjs';
import { testSpace_createAndInit, testSpace_persistTransformResult, testSpace_putGetDelete, testSpace_registerNewIbGib_GetLatest, } from '../space-respec-helper.mjs';
import { InnerSpace_V1, } from './inner-space-v1.mjs';
import { DEFAULT_INNER_SPACE_DATA_V1 } from './inner-space-types.mjs';
import { putInSpace, getFromSpace } from '../space-helper.mjs';
const logalot = true;
await respecfully(sir, `[${InnerSpace_V1.name} specs]`, async () => {
    await testSpace_createAndInit({
        respecfulTitle: sir,
        // shortcircuitRespec: true,
        fnGetInitialSpaceData: async () => {
            return {
                ...DEFAULT_INNER_SPACE_DATA_V1,
                name: (await getUUID()).substring(0, 8),
                uuid: await getUUID(),
            };
        },
        fnGetInitialSpaceRel8ns: () => {
            return Promise.resolve(undefined);
        },
        fnGetSpace: (initialData, initialRel8ns) => {
            return Promise.resolve(new InnerSpace_V1(initialData, initialRel8ns));
        },
    });
    await testSpace_putGetDelete({
        respecfulTitle: sir,
        // shortcircuitRespec: true,
        fnGetInitialSpaceData: async () => {
            return {
                ...DEFAULT_INNER_SPACE_DATA_V1,
                name: (await getUUID()).substring(0, 8),
                uuid: await getUUID(),
            };
        },
        fnGetInitialSpaceRel8ns: () => {
            return Promise.resolve(undefined);
        },
        fnGetSpace: (initialData, initialRel8ns) => {
            return Promise.resolve(new InnerSpace_V1(initialData, initialRel8ns));
        },
    });
    await testSpace_persistTransformResult({
        respecfulTitle: sir,
        // shortcircuitRespec: true,
        fnGetInitialSpaceData: async () => {
            return {
                ...DEFAULT_INNER_SPACE_DATA_V1,
                name: (await getUUID()).substring(0, 8),
                uuid: await getUUID(),
            };
        },
        fnGetInitialSpaceRel8ns: () => {
            return Promise.resolve(undefined);
        },
        fnGetSpace: (initialData, initialRel8ns) => {
            return Promise.resolve(new InnerSpace_V1(initialData, initialRel8ns));
        },
    });
    await testSpace_registerNewIbGib_GetLatest({
        respecfulTitle: sir,
        // shortcircuitRespec: true,
        fnGetInitialSpaceData: async () => {
            return {
                ...DEFAULT_INNER_SPACE_DATA_V1,
                name: (await getUUID()).substring(0, 8),
                uuid: await getUUID(),
            };
        },
        fnGetInitialSpaceRel8ns: () => {
            return Promise.resolve(undefined);
        },
        fnGetSpace: (initialData, initialRel8ns) => {
            return Promise.resolve(new InnerSpace_V1(initialData, initialRel8ns));
        },
        testLatestIterations: 5,
    });
    await respecfully(sir, 'Advanced Operations (mut8, rel8, fork)', async () => {
        await ifWe(sir, 'should handle mut8 transform and persistence', async () => {
            const space = new InnerSpace_V1(DEFAULT_INNER_SPACE_DATA_V1, undefined);
            const gib = Factory_V1.primitive({ ib: 'gib' });
            // 1. Create Origin
            const resOrigin = await Factory_V1.firstGen({
                ib: 'origin',
                data: { initial: 'data' },
                dna: true,
                parentIbGib: gib,
            });
            const origin = resOrigin.newIbGib;
            // 2. Mut8 (Mutate)
            const resMut8 = await mut8({
                src: origin,
                dataToAddOrPatch: { mutated: 'data' },
                dna: true,
            });
            const mutated = resMut8.newIbGib;
            // 3. Put in space (should handle both if persisted correctly)
            const resPut = await putInSpace({
                space,
                ibGibs: [origin, mutated],
            });
            iReckon(sir, resPut.success).isGonnaBeTrue();
            // 4. Verify get
            const resGet = await getFromSpace({
                space,
                addrs: [getIbGibAddr({ ibGib: mutated })],
            });
            iReckon(sir, resGet.success).isGonnaBeTrue();
            // Using non-null assertion as we verified success
            iReckon(sir, resGet.ibGibs[0].data.mutated).isGonnaBe('data');
        });
        await ifWe(sir, 'should handle rel8 transform and persistence', async () => {
            const space = new InnerSpace_V1(DEFAULT_INNER_SPACE_DATA_V1, undefined);
            const gib = Factory_V1.primitive({ ib: 'gib' });
            // 1. Create A and B
            const resA = await Factory_V1.firstGen({ ib: 'A', dna: true, parentIbGib: gib });
            const A = resA.newIbGib;
            const resB = await Factory_V1.firstGen({ ib: 'B', dna: true, parentIbGib: gib });
            const B = resB.newIbGib;
            const bAddr = getIbGibAddr({ ibGib: B });
            // 2. Rel8 A -> B (A links to B)
            const resRel8 = await rel8({
                src: A,
                rel8nsToAddByAddr: { 'linked_to': [bAddr] },
                dna: true,
            });
            const A_linked = resRel8.newIbGib;
            // 3. Put in space
            await putInSpace({
                space,
                ibGibs: [A, B, A_linked],
            });
            // 4. Verify relation
            const resGet = await getFromSpace({
                space,
                addr: getIbGibAddr({ ibGib: A_linked }),
            });
            const retrieved = resGet.ibGibs[0];
            iReckon(sir, retrieved.rel8ns.linked_to).includes(bAddr);
        });
        await ifWe(sir, 'should handle fork persistence', async () => {
            const space = new InnerSpace_V1(DEFAULT_INNER_SPACE_DATA_V1, undefined);
            const gib = Factory_V1.primitive({ ib: 'gib' });
            // 1. Create Root
            const resRoot = await Factory_V1.firstGen({ ib: 'root', dna: true, parentIbGib: gib });
            const root = resRoot.newIbGib;
            // 2. Fork
            const resFork = await fork({
                src: root,
                dna: true,
                tjp: { timestamp: true },
            });
            const forkIbGib = resFork.newIbGib;
            // 3. Put
            await putInSpace({ space, ibGibs: [root, forkIbGib] });
            // 4. Verify
            const resGet = await getFromSpace({
                space,
                addr: getIbGibAddr({ ibGib: forkIbGib })
            });
            iReckon(sir, resGet.success).isGonnaBeTrue();
        });
    });
});
//# sourceMappingURL=inner-space-v1.respec.mjs.map