import{join}from"node:path";import{readFile}from"node:fs/promises";import{isErrnoCode}from"#shared/guards.js";import{atomicWriteFile}from"#shared/atomic-write-file.js";async function writeOutputPublicationJournal(e,t){await writeJournal(e,t)}async function readOutputPublicationJournal(e){let t=await readJournal(e);return isOutputPublicationJournal(t)?t:void 0}async function writeRecoveryLeaseJournal(e,t){await writeJournal(e,t)}async function readRecoveryLeaseJournal(e){let t=await readJournal(e);return isRecoveryLeaseJournal(t)?t:void 0}function resolveJournalFilePath(t){return join(t,`owner.json`)}async function writeJournal(e,t){await atomicWriteFile(resolveJournalFilePath(e),`${JSON.stringify(t,null,2)}\n`)}async function readJournal(e){try{return JSON.parse(await readFile(resolveJournalFilePath(e),`utf8`))}catch(e){if(isErrnoCode(e,`ENOENT`)||e instanceof SyntaxError)return;throw e}}function isOutputPublicationJournal(e){if(typeof e!=`object`||!e)return!1;let t=e;return typeof t.finalOutputDir==`string`&&typeof t.finalSummaryPath==`string`&&typeof t.hadOutput==`boolean`&&typeof t.hadSummary==`boolean`&&(t.liveness===`active`||t.liveness===`recoverable`)&&typeof t.outputBackupPath==`string`&&[`acquired`,`prepared`,`backed-up`,`committed`].includes(t.phase??``)&&typeof t.pid==`number`&&typeof t.scratchDir==`string`&&typeof t.stagedOutputDir==`string`&&typeof t.stagedSummaryPath==`string`&&typeof t.summaryBackupPath==`string`&&typeof t.token==`string`}function isRecoveryLeaseJournal(e){if(typeof e!=`object`||!e)return!1;let t=e;return typeof t.pid==`number`&&typeof t.token==`string`}export{readOutputPublicationJournal,readRecoveryLeaseJournal,resolveJournalFilePath,writeOutputPublicationJournal,writeRecoveryLeaseJournal};