//!wrt $BSPEC:{"icn":null,"cpr":"Copyright (C) Windows 96 Team 2021.","dsc":"","frn":"Recycle Bin UI","ver":1,"ssy":"gui"}

const{Theme:Theme,DialogCreator:DialogCreator,OperationDialog:OperationDialog}=w96.ui,systemFolders=["/trash","/system"],systemDrive="C:",trashPath="C:/trash";function pathFix(t){return t.replaceAt(0,t[0].toUpperCase())}class RecycleUIApplication extends WApplication{constructor(){super()}async main(t){if(await super.main(t),t.shift(),t.forEach(((t,e,r)=>r[e]=pathFix(t))),await FS.exists(trashPath)||await FS.mkdir(trashPath),0==t.length)return;const e=FSUtil.getParentPath(t[0]);for(let r of t){if(FSUtil.getParentPath(r)!==e){const t=DialogCreator.create({title:"Error",body:"General failure.",icon:"error"});return await t.wait(),1}}let r,a,i,o="permanent";for(let e of t){if(null==e||!await FS.exists(e)){const t=DialogCreator.create({title:"Error",body:"Invalid argument.",icon:"error"});return await t.wait(),1}const t=await FSUtil.deconstructFullPath(e);if("/"==t.path){const t=DialogCreator.create({title:"Error",body:"You cannot recycle a drive root.",icon:"error"});return await t.wait(),1}if("C:"==t.prefix&&systemFolders.includes(t.path)){const t=DialogCreator.create({title:"Error",body:"Unable to delete system folder.",icon:"error"});return await t.wait(),1}e.startsWith("C:/trash/")||(o="trash")}if(1==t.length){const e=await FS.isFile(t[0]);"trash"==o?(r=await Theme.getIconUrl("status/trash-"+(e?"file":"dir"),"32x32"),i=`Are you sure you want to send '${FSUtil.fname(t[0])}' to the Recycle Bin?`):i=`Are you sure you want to delete '${FSUtil.fname(t[0])}'?`,a=`Confirm ${e?"File":"Folder"} Delete`}else a="Confirm Multiple Item Delete","trash"==o?(r=await Theme.getIconUrl("status/trash-dir","32x32"),i=`Are you sure you want to send these ${t.length} items to the Recycle Bin?`):i=`Are you sure you want to delete these ${t.length} items?`;"permanent"==o&&(r=await Theme.getIconUrl("actions/delete-permanent"));const n=this.createWindow({title:a,taskbar:!1,resizable:!1,controlBoxStyle:"WS_CBX_CLOSE",initialWidth:412,initialHeight:145,center:!0,bodyClass:"app-dlg-recycle",body:`<div class="content">\n                <div class="icon" style="background-image: url(${r})"></div>\n                <div class="text">${i}</div>\n            </div>\n            <div class="buttons">\n                <button class="w96-button yes">Yes</button>\n                <button class="w96-button no">No</button>\n            </div>`,animations:{windowClose:"anim-null",windowOpen:"anim-null"}},!0),s=n.getBodyContainer(),l=s.querySelector(".buttons>.yes");l.addEventListener("click",(()=>this.processDeletions(t,o))),s.querySelector(".buttons>.no").addEventListener("click",(()=>n.close())),n.onshown=()=>l.focus(),n.show(),this.mainwnd=n}async processDeletions(t,e){this.mainwnd.setVisible(!1);try{"trash"==e?await OperationDialog.recycle(t):await OperationDialog.delete(t,"trash")}catch(t){return DialogCreator.create({title:"Error",body:`${t}`,icon:"error"}),0}this.mainwnd.close(),this.terminate()}ontermination(){super.ontermination()}}

return await WApplication.execAsync(new RecycleUIApplication(), this.boxedEnv.args, this);