const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./BiCEaioG2.js","./CG84LPq-.js","./DrKWWDpL.js","./DsQIgu-4.js","./2PQNgpBd.js","./DsanjglN.js","./CbGRcr-D.js","./DgZn9uJn.js","./CudpqN6Z.js","./DYP3EJMY.js","./DTkg6Mnx.js","./DIQmuJJZ.js","./D7w7HllH.js","./DB5ifLM-.js","./CX8DOZuw2.js","./DOqzs8kl2.js","./CxlOpib82.js","./BmWTAsFW.js","./DeO5NWhN.js","./Czb-RtcG.js","./C0nwgWmB.js","./B5XQZRTz.js","./CWxOfblT.js","./CVwAhlPZ.js","./BVC3q6GM.js","./B8PRMiR2.js","./BDDiPmhF.js","./BYJC6_MM.js","./bG-fsHMS.js","./D-eE9s2f.js","./DlZwu-Y7.js","./Cx_-1PHK2.js","./B_DA9RKE2.js","./DOPTvi6T.js","./CvaH7m0y.js","./O2P-f9sv.js","./BQ9wXJVQ2.js","./OnYyYdSr2.js","./BYXFvmSx.js"])))=>i.map(i=>d[i]);
import{s as e}from"./DrKWWDpL.js";import{n as t,t as n}from"./DsQIgu-4.js";import{t as r}from"./DYP3EJMY.js";import{Hn as i}from"./DIQmuJJZ.js";import{t as a}from"./CxlOpib82.js";import{u as o}from"./CX8DOZuw2.js";import{n as s}from"./DVQI6VOy.js";import{c}from"./CvaH7m0y.js";import"./DOPTvi6T.js";import{n as l}from"./C1rhEBqL.js";var u=e(r(),1);t();var d=`task_id_sequence`;function f(e){return(0,u.join)(o(e),`.cleo`,`.sequence.json`)}function p(e){return(0,u.join)(o(e),`.cleo`,`.sequence`)}function m(e){if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.counter==`number`&&typeof t.lastId==`string`&&typeof t.checksum==`string`}function h(e){return e.counter===0&&e.lastId===`T000`&&e.checksum===`seed`}function g(e){if(!(0,u.existsSync)(e))return null;try{let t=JSON.parse((0,u.readFileSync)(e,`utf-8`));return m(t)?t:null}catch{return null}}function _(e){if(!(0,u.existsSync)(e))return;let t=`${e}.migrated`;try{if(!(0,u.existsSync)(t)){(0,u.renameSync)(e,t);return}(0,u.renameSync)(e,`${t}.${Date.now()}`)}catch{}}async function v(e,t){if(t){let e=await t.getMetaValue(d);return m(e)?e:null}let{getDb:r}=await n(async()=>{let{getDb:e}=await import(`./BiCEaioG2.js`);return{getDb:e}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37]),import.meta.url),{eq:i}=await n(async()=>{let{eq:e}=await import(`./BYXFvmSx.js`);return{eq:e}},__vite__mapDeps([38,5,6]),import.meta.url),a=(await(await r(e)).select().from(c).where(i(c.key,d)).all())[0]?.value;if(!a)return null;try{let e=JSON.parse(a);return m(e)?e:null}catch{return null}}async function y(e,t,n){if(n){await n.setMetaValue(d,e);return}await l(t,d,e)}async function b(e,t,n){let r=f(t),i=p(t),a=[g(r),g(i)].filter(e=>e!==null);if(a.length===0)return e;let o=a.reduce((e,t)=>t.counter>e.counter?t:e),s=!e||h(e)||o.counter>e.counter;return s&&await y(o,t,n),_(r),_(i),s?o:e}async function x(e,t){return b(await v(e,t),e,t)}function S(e){let t=0;for(let n of e){let e=n.id.match(/^T(\d+)$/);if(e){let n=parseInt(e[1],10);n>t&&(t=n)}}return t}async function C(e,t){let n=null,r=t??await s(e);t||(n=r);try{let{tasks:e}=await r.queryTasks({}),t=await r.loadArchive();return[...e??[],...t?.archivedTasks??[]]}finally{n&&await n.close()}}async function w(e,t){let n=await x(e,t);if(!n)return{valid:!1,counter:0,maxIdInData:0,missing:!0};let r=S(await C(e,t)),i=n.counter>=r;return{counter:n.counter,maxIdInData:r,valid:i,...i?{}:{issue:`Counter (${n.counter}) is behind max ID (T${r})`,fix:`Run cleo sequence repair`}}}async function T(e,t){let n=await x(e,t),r=S(await C(e,t)),i=n?.counter??0;if(!n){let n={counter:r,lastId:`T${r}`,checksum:`seed-${Date.now()}`};return await y(n,e,t),{repaired:!0,counter:n.counter,oldCounter:i,newCounter:n.counter,message:`Sequence initialized at ${n.counter}`}}if(i>=r)return{repaired:!1,message:`Sequence already valid`,counter:i,oldCounter:i,newCounter:i};let a=r;return await y({counter:a,lastId:`T${a}`,checksum:`repair-${Date.now()}`},e,t),{repaired:!0,counter:a,oldCounter:i,newCounter:a,message:`Sequence repaired: ${i} -> ${a}`}}var E=3;async function D(e,t=0){let{getDb:r,getNativeDb:o}=await n(async()=>{let{getDb:e,getNativeDb:t}=await import(`./BiCEaioG2.js`);return{getDb:e,getNativeDb:t}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37]),import.meta.url);await r(e);let s=o();if(!s)throw new a(i.FILE_ERROR,`Native database not available for atomic ID allocation`);let c=`_cleo_seq_alloc_${Date.now()}_${t}`;s.prepare(`SAVEPOINT ${c}`).run();try{s.prepare(`
      UPDATE schema_meta
      SET value = json_set(value,
        '$.counter', json_extract(value, '$.counter') + 1,
        '$.lastId', 'T' || printf('%03d', json_extract(value, '$.counter') + 1),
        '$.checksum', 'alloc-' || strftime('%s','now')
      )
      WHERE key = 'task_id_sequence'
    `).run();let n=s.prepare(`
      SELECT json_extract(value, '$.counter') AS counter
      FROM schema_meta WHERE key = 'task_id_sequence'
    `).get();if(!n)throw new a(i.FILE_ERROR,`Sequence counter not found after increment`);let r=`T${String(n.counter).padStart(3,`0`)}`;if(s.prepare(`SELECT id FROM tasks_tasks WHERE id = ?`).get(r)){if(s.prepare(`ROLLBACK TO SAVEPOINT ${c}`).run(),s.prepare(`RELEASE SAVEPOINT ${c}`).run(),t>=E)throw new a(i.ID_COLLISION,`Failed to allocate unique task ID after ${E} retries (last attempted: ${r})`);return await T(e),D(e,t+1)}return s.prepare(`RELEASE SAVEPOINT ${c}`).run(),r}catch(e){try{s.prepare(`ROLLBACK TO SAVEPOINT ${c}`).run(),s.prepare(`RELEASE SAVEPOINT ${c}`).run()}catch{}throw e}}export{w as n,T as r,D as t};