import{g as k,s as f}from"./store-aiW_fD0x.js";const d="lvonce-bookmarks-v2",i="root";function l(o=new Date().toISOString()){return{version:2,folders:[{id:i,name:"全部书签",parentId:null,createdAt:o}],bookmarks:[]}}async function c(){const o=await k(d);if(!o||!Array.isArray(o.bookmarks))return l();const r=Array.isArray(o.folders)?o.folders.filter(w):[];return r.some(t=>t?.id===i)||r.unshift(l().folders[0]),{version:2,folders:r,bookmarks:o.bookmarks.filter(p)}}async function y(o,r,t=i){const e=u(r);if(!e)throw new Error("请输入有效的 http/https 网址");const n=await c();if(n.bookmarks.some(s=>s.url===e))throw new Error("这个网址已经收藏");const a=new Date().toISOString();n.bookmarks.unshift({id:crypto.randomUUID(),title:o.trim()||new URL(e).hostname,url:e,note:"",folderId:n.folders.some(s=>s.id===t)?t:i,createdAt:a,updatedAt:a}),await f(d,n)}async function b(o,r=i){const t=o.trim();if(!t)throw new Error("请输入分类名称");const e=await c(),n=e.folders.some(s=>s.id===r)?r:i;if(e.folders.some(s=>s.parentId===n&&s.name===t))throw new Error("同级已有这个分类");const a=crypto.randomUUID();return e.folders.push({id:a,name:t,parentId:n,createdAt:new Date().toISOString()}),await f(d,e),a}async function g(o,r){if(o===i)throw new Error("根分类不能重命名");const t=r.trim();if(!t)throw new Error("请输入分类名称");const e=await c(),n=e.folders.find(a=>a.id===o);if(n){if(e.folders.some(a=>a.id!==o&&a.parentId===n.parentId&&a.name===t))throw new Error("同级已有这个分类");n.name=t,await f(d,e)}}async function I(o){if(o===i)return;const r=await c(),t=r.folders.find(n=>n.id===o);if(!t)return;const e=m(r.folders,o);r.folders=r.folders.filter(n=>!e.has(n.id));for(const n of r.bookmarks)e.has(n.folderId)&&(n.folderId=t.parentId??i);await f(d,r)}async function S(o,r){const t=await c();if(!t.folders.some(n=>n.id===r))return;const e=t.bookmarks.find(n=>n.id===o);!e||e.folderId===r||(e.folderId=r,e.updatedAt=new Date().toISOString(),await f(d,t))}async function E(o,r){if(o===i||o===r)return;const t=await c(),e=t.folders.find(s=>s.id===o),n=t.folders.find(s=>s.id===r);if(!e||!n||e.parentId===r)return;if(m(t.folders,o).has(r))throw new Error("不能移动到自己的子分类下");if(t.folders.some(s=>s.id!==o&&s.parentId===r&&s.name===e.name))throw new Error("目标分类下已有同名分类");e.parentId=r,await f(d,t)}async function A(o){const r=await c(),t=r.bookmarks.filter(e=>e.id!==o);t.length!==r.bookmarks.length&&await f(d,{...r,bookmarks:t})}function u(o){try{const r=new URL(o);return r.protocol==="http:"||r.protocol==="https:"?r.toString():null}catch{return null}}function m(o,r){const t=new Set([r]);let e=!0;for(;e;){e=!1;for(const n of o)n.parentId&&t.has(n.parentId)&&!t.has(n.id)&&(t.add(n.id),e=!0)}return t}function w(o){if(!o||typeof o!="object")return!1;const r=o;return typeof r.id=="string"&&typeof r.name=="string"&&(typeof r.parentId=="string"||r.parentId===null)&&typeof r.createdAt=="string"}function p(o){if(!o||typeof o!="object")return!1;const r=o;return typeof r.id=="string"&&typeof r.title=="string"&&typeof r.url=="string"&&typeof r.folderId=="string"}export{d as B,i as R,b as a,y as b,I as c,A as d,E as e,c as l,S as m,g as r};
