/**
 * Session codec — Hermes uses a single sessionId for cross-heartbeat continuity
 * via the --resume CLI flag. Same shape as the Hermes adapter.
 */

function readNonEmptyString(value) {
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}

export const sessionCodec = {
  deserialize(raw) {
    if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return null;
    const sessionId =
      readNonEmptyString(raw.sessionId) ?? readNonEmptyString(raw.session_id);
    if (!sessionId) return null;
    return { sessionId };
  },
  serialize(params) {
    if (!params) return null;
    const sessionId =
      readNonEmptyString(params.sessionId) ?? readNonEmptyString(params.session_id);
    if (!sessionId) return null;
    return { sessionId };
  },
  getDisplayId(params) {
    if (!params) return null;
    return readNonEmptyString(params.sessionId) ?? readNonEmptyString(params.session_id);
  },
};
