import { extractErrorMsg } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
import { SYNC_SAGA_MSG_ATOM } from "./sync-saga-message-constants.mjs";
/**
 * Constructs the standard 'ib' string for a Sync Saga Message stone.
 */
export async function getSyncSagaMessageIb({ data, }) {
    const lc = `[${getSyncSagaMessageIb.name}]`;
    try {
        // sync_saga_msg sagaId stage
        const ib = [
            SYNC_SAGA_MSG_ATOM,
            data.sagaId,
            data.stage
        ].join(' ');
        // console.log(`${lc} constructed ib: ${ib}`); // temporary debug
        return ib;
    }
    catch (error) {
        console.error(`${lc} ${extractErrorMsg(error)}`);
        throw error;
    }
}
/**
 * Parses a standard Sync Saga Message 'ib' string.
 */
export async function parseSyncSagaMessageIb({ ib, }) {
    const lc = `[${parseSyncSagaMessageIb.name}]`;
    try {
        const parts = ib.split(' ');
        if (parts.length !== 3) {
            throw new Error(`Invalid sync saga message ib. Expected 3 parts [atom sagaId stage]. Got ${parts.length}. (E: 7abec847426bd8f41a475cafcc118826)`);
        }
        const [atom, sagaId, stage] = parts;
        if (atom !== SYNC_SAGA_MSG_ATOM) {
            throw new Error(`Atom mismatch. Expected ${SYNC_SAGA_MSG_ATOM}. Got ${atom}. (E: 1b4ddf426ed87e474816ed48cedb8a26)`);
        }
        return { atom, sagaId, stage };
    }
    catch (error) {
        console.error(`${lc} ${extractErrorMsg(error)}`);
        throw error;
    }
}
import { getRel8dIbGibs } from "../../common/other/ibgib-helper.mjs";
import { SYNC_MSG_REL8N_NAME } from "../sync-constants.mjs";
export async function getSyncSagaMessageFromFrame({ frameIbGib, space, }) {
    const lc = `[${getSyncSagaMessageFromFrame.name}]`;
    try {
        const rel8d = await getRel8dIbGibs({
            ibGib: frameIbGib,
            rel8nNames: [SYNC_MSG_REL8N_NAME],
            space
        });
        const msgs = rel8d[SYNC_MSG_REL8N_NAME];
        if (!msgs || msgs.length === 0) {
            throw new Error(`(UNEXPECTED) No sync message stone found found for frame: ${frameIbGib.ib} (E: 8a4b2c1d3e5f6a9b7c8d9e0f1a2b3c4d)`);
        }
        if (msgs.length > 1) {
            throw new Error(`(UNEXPECTED) Multiple sync message stones found (${msgs.length}) for frame: ${frameIbGib.ib}. logic not currently equipped to handle multiple messages per frame. (E: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d)`);
        }
        return msgs[0];
    }
    catch (error) {
        console.error(`${lc} ${extractErrorMsg(error)}`);
        throw error;
    }
}
//# sourceMappingURL=sync-saga-message-helpers.mjs.map