import{n as e}from"./index-7yDgay5u.js";import{c as t}from"./attachment-encryption-DTWbJYgF.js";import{a as n,s as r}from"./fileAssetService-gA4Q8V3n.js";import{m as i}from"./selectors-DZWoTA9D.js";async function a(e,t){s(t);let n={completed:0,total:e.items.length,textImported:0,textSkipped:0,assetImported:0,assetSkipped:0},r=l(t.diaryService),i=()=>{n.completed+=1,t.onProgress?.({...n})},a=[];for(let o of e.items)o.type===`text`&&(r.has(u(e.source.type,o.id))?n.textSkipped+=1:(a.push({notebookId:t.notebookId,text:o.text,createdAt:o.createdAt,externalSource:e.source.type,externalId:o.id}),r.add(u(e.source.type,o.id)),n.textImported+=1),i());a.length>0&&t.diaryService.addTextEntriesWithOptions(a);let d=new Map;for(let t of e.items){if(t.type!==`asset`)continue;if(r.has(u(e.source.type,t.id))){n.assetSkipped+=1,i();continue}let a=d.get(t.filename);a?a.push(t):d.set(t.filename,[t])}for await(let a of e.streamAssets()){let s=d.get(a.filename);if(s){for(let l of s)c(l,{...await o(l,a,t),createdAt:l.createdAt},e.source.type,t),r.add(u(e.source.type,l.id)),n.assetImported+=1,i();d.delete(a.filename)}}for(let e of d.values()){n.assetSkipped+=e.length;for(let t of e)i()}return t.onProgress?.({...n}),n}async function o(e,i,a){let o=new Blob([t(i.content)],{type:i.mimeType??``});return e.mediaType===`video`?r(await a.fileAssetService.uploadVideo(o,{mimeType:i.mimeType}),a.notebookId):n(await a.fileAssetService.uploadImage(o,{thumbnail:!0}),a.notebookId)}function s(t){if(!i(t.diaryService.modelState,t.notebookId))throw Error(e(`settings.import.targetNotebookUnavailable`,`Target notebook is unavailable. Choose another notebook.`))}function c(e,t,n,r){r.diaryService.addAttachmentEntry({attachment:{...t,notebookId:r.notebookId},createdAt:e.createdAt,externalSource:n,externalId:e.id})}function l(e){let t=new Set;for(let n of e.modelState.entries)!n.externalSource||!n.externalId||t.add(u(n.externalSource,n.externalId));return t}function u(e,t){return`${e}\n${t}`}export{a as t};
//# sourceMappingURL=importPackage-QNP382Tt.js.map