/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/

var c=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var x=(s,t)=>{for(var a in t)c(s,a,{get:t[a],enumerable:!0})},T=(s,t,a,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of S(t))!y.call(s,n)&&n!==a&&c(s,n,{get:()=>t[n],enumerable:!(e=E(t,n))||e.enumerable});return s};var D=s=>T(c({},"__esModule",{value:!0}),s);var I={};x(I,{DEFAULT_SETTINGS:()=>f,default:()=>d,downloadAndSaveImage:()=>v,generateUserFriendlyFilename:()=>F,getUrlFromClipboard:()=>b,isImageUrl:()=>w});module.exports=D(I);var i=require("obsidian"),f={confirmBeforeEmbed:!0,showFilePath:!1,attachmentFolder:"",filenameFormat:"{name}-{timestamp}",useTimestamp:!0};function w(s){let t=[".jpg",".jpeg",".png",".gif",".webp",".svg",".bmp",".tiff"];try{let a=new URL(s);return t.some(e=>a.pathname.toLowerCase().endsWith(e))}catch(a){return!1}}function b(s){if(!s)return null;let t=s.getData("text/plain");if(!t)return null;try{return new URL(t),t}catch(a){return null}}function F(s,t){var m;let e=new URL(s).pathname.split("/").pop()||"image";e=e.split("?")[0].split("#")[0];let n=((m=e.split(".").pop())==null?void 0:m.toLowerCase())||"jpg";e=e.slice(0,-(n.length+1)),e=e.replace(/[^a-zA-Z0-9]+/g,"-").replace(/^-+|-+$/g,"").toLowerCase(),e||(e="image");let r=new Date().toISOString().replace(/[:.]/g,"-").slice(0,19),o=new Date().toISOString().split("T")[0],l=t.filenameFormat.replace("{name}",e).replace("{timestamp}",t.useTimestamp?r:"").replace("{date}",o).replace(/-+/g,"-").replace(/-+$/g,"");return l.trim()||(l=e),`${l}.${n}`}async function P(s,t){return s.vault.getAbstractFileByPath(t)||await s.vault.createFolder(t),t}async function v(s,t,a,e){try{await P(s,a);let n=F(t,e),r=`${a}/${n}`,o=await fetch(t);if(!o.ok)throw new Error(`Failed to download image: ${o.statusText}`);let l=await o.arrayBuffer();return await s.vault.createBinary(r,l),r}catch(n){throw console.error("Error downloading image:",n),n}}var d=class extends i.Plugin{async onload(){await this.loadSettings(),this.settings.attachmentFolder||(this.settings.attachmentFolder=this.app.vault.config.attachmentFolderPath||"attachments",await this.saveSettings()),this.registerEvent(this.app.workspace.on("editor-paste",async(a,e,n)=>{let r=b(a.clipboardData);if(r&&w(r)){a.preventDefault();try{if(this.settings.confirmBeforeEmbed&&!await new Promise(p=>{let g=new i.Notice("Download and embed this image?",0),u=g.noticeEl.createDiv({cls:"notice-buttons"});u.createEl("button",{text:"Yes",cls:"mod-cta"}).addEventListener("click",()=>{g.hide(),p(!0)}),u.createEl("button",{text:"No"}).addEventListener("click",()=>{g.hide(),p(!1)})}))return;let o=await v(this.app,r,this.settings.attachmentFolder,this.settings),l=`![[${o}]]`;e.replaceSelection(l);let m=this.settings.showFilePath?`Image saved and embedded: ${o}`:"Image saved and embedded successfully!";new i.Notice(m,3e3)}catch(o){console.error("Error processing image:",o),new i.Notice("Failed to download and embed image. Check console for details.",5e3)}}})),this.addSettingTab(new h(this.app,this))}onunload(){}async loadSettings(){this.settings=Object.assign({},f,await this.loadData())}async saveSettings(){await this.saveData(this.settings)}},h=class extends i.PluginSettingTab{constructor(a,e){super(a,e);this.plugin=e}display(){let{containerEl:a}=this;a.empty(),a.createEl("h1",{text:"General Settings"}),new i.Setting(a).setName("Confirm before embedding").setDesc("Show a confirmation dialog before downloading and embedding images").addToggle(e=>e.setValue(this.plugin.settings.confirmBeforeEmbed).onChange(async n=>{this.plugin.settings.confirmBeforeEmbed=n,await this.plugin.saveSettings()})),new i.Setting(a).setName("Show file path").setDesc("Show the saved file path in the success notice").addToggle(e=>e.setValue(this.plugin.settings.showFilePath).onChange(async n=>{this.plugin.settings.showFilePath=n,await this.plugin.saveSettings()})),new i.Setting(a).setName("Attachment folder").setDesc("Folder where downloaded images will be saved (relative to vault root). Leave empty to use Obsidian's default attachment folder.").addText(e=>e.setPlaceholder("Use Obsidian default").setValue(this.plugin.settings.attachmentFolder).onChange(async n=>{this.plugin.settings.attachmentFolder=n,await this.plugin.saveSettings()})),new i.Setting(a).setName("Filename format").setDesc("Format for saved filenames. Available placeholders: {name}, {timestamp}, {date}").addText(e=>e.setPlaceholder("{name}-{timestamp}").setValue(this.plugin.settings.filenameFormat).onChange(async n=>{this.plugin.settings.filenameFormat=n,await this.plugin.saveSettings()})),new i.Setting(a).setName("Use timestamp").setDesc("Add timestamp to filenames for uniqueness").addToggle(e=>e.setValue(this.plugin.settings.useTimestamp).onChange(async n=>{this.plugin.settings.useTimestamp=n,await this.plugin.saveSettings()}))}};

/* nosourcemap */