/**
 * @module metaspace-innerspace
 *
 * in-memory metaspace, for running ibgib apps in memory only, where the
 * underlying data spaces do not use persistent storage mediums.
 */
import { getUUID, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { GLOBAL_LOG_A_LOT, GLOBAL_TIMER_NAME } from '../../../../core-constants.mjs';
import { InnerSpace_V1 } from '../../../../witness/space/inner-space/inner-space-v1.mjs';
import { fnCreateNewLocalSpace, fnDtoToSpace, registerInnerSpace } from './metaspace-innerspace-helper.mjs';
import { MetaspaceBase } from '../metaspace-base.mjs';
const logalot = GLOBAL_LOG_A_LOT;
/**
 * for use when creating a metaspace that only resides in memory, i.e.,
 * doesn't live on a persistent backing store like a filesystem.
 */
export class Metaspace_Innerspace extends MetaspaceBase {
    cacheSvc;
    // we won't get an object back, only a DTO ibGib essentially
    lc = `[${Metaspace_Innerspace.name}]`;
    /**
     * since this is an in-memory metaspace, we always return the same instance
     * for the zero space.
     */
    _zeroSpace;
    /**
     * since this is an in-memory metaspace, we always return the same instance
     *
     * returns this._zeroSpace
     */
    get zeroSpace() {
        const lc = `${this.lc}[get zeroSpace]`;
        if (!this._zeroSpace) {
            throw new Error(`${lc} this._zeroSpace falsy. should have been created on this.initialize (E: e723177fd2a74e25eb25e2299dfa4d23)`);
        }
        return this._zeroSpace;
    }
    constructor(cacheSvc) {
        super(cacheSvc);
        this.cacheSvc = cacheSvc;
        const lc = `${this.lc}[ctor]`;
        if (logalot) {
            console.log(`${lc} ${GLOBAL_TIMER_NAME}`);
            console.timeLog(GLOBAL_TIMER_NAME);
            console.log(`${lc} created.`);
        }
    }
    async initializeMetaspaceFactory({ metaspaceFactory }) {
        const lc = `[${this.initializeMetaspaceFactory.name}]`;
        try {
            if (logalot) {
                console.log(`${lc} starting... (I: 5a29670439ac47634dcdfe25225c8223)`);
            }
            metaspaceFactory ??= {};
            metaspaceFactory.fnDefaultLocalSpaceFactory ??=
                (opts) => fnCreateNewLocalSpace(opts);
            metaspaceFactory.fnDtoToSpace ??=
                (spaceDto) => fnDtoToSpace(spaceDto);
            metaspaceFactory.fnZeroSpaceFactory ??= () => {
                if (this._zeroSpace) {
                    return this._zeroSpace;
                }
                else {
                    // return this._zeroSpace!
                    const zeroSpace = new InnerSpace_V1(/*initialData*/ undefined, /*initialRel8ns*/ undefined);
                    zeroSpace.gib = 'gib';
                    this._zeroSpace = zeroSpace;
                    registerInnerSpace(this._zeroSpace);
                    return zeroSpace;
                }
            };
            await super.initializeMetaspaceFactory({ metaspaceFactory });
            if (this.metaspaceFactory.fnZeroSpaceFactory) {
                this._zeroSpace = this.metaspaceFactory.fnZeroSpaceFactory();
            }
            else {
                this._zeroSpace = await fnCreateNewLocalSpace({
                    allowCancel: false,
                    spaceName: 'in-memory-space_' + (await getUUID()).slice(0, 10),
                    logalot,
                    getFnPrompt: () => this.getFnPrompt(),
                });
                if (!this._zeroSpace) {
                    throw new Error(`(UNEXPECTED) create this._zeroSpace is falsy but no error after create? (E: 63c27616be41564377f843baa6725423)`);
                }
                // don't think this is necessary but...
                // metaspaceFactory.fnZeroSpaceFactory = () => {
                //     // return this._zeroSpace!
                //     const zeroSpace = new InnerSpace_V1(/*initialData*/ undefined as any, /*initialRel8ns*/ undefined);
                //     zeroSpace.gib = 'gib';
                //     return zeroSpace;
                // };
            }
            await this._zeroSpace.initialized;
        }
        catch (error) {
            console.error(`${lc} ${error.message}`);
            throw error;
        }
        finally {
            if (logalot) {
                console.log(`${lc} complete.`);
            }
        }
    }
}
//# sourceMappingURL=metaspace-innerspace.mjs.map