import e from"../../is-plain-object/index.es.js";import{Editor as t,Operation as s,Path as i}from"../../slate/dist/index.es.js";var o={isHistory:t=>e(t)&&Array.isArray(t.redos)&&Array.isArray(t.undos)&&(0===t.redos.length||s.isOperationList(t.redos[0]))&&(0===t.undos.length||s.isOperationList(t.undos[0]))},r=new WeakMap,n=new WeakMap,a={isHistoryEditor:e=>t.isEditor(e)&&o.isHistory(e.history),isMerging:e=>n.get(e),isSaving:e=>r.get(e),redo(e){e.redo()},undo(e){e.undo()},withoutMerging(e,t){var s=a.isMerging(e);n.set(e,!1),t(),n.set(e,s)},withoutSaving(e,t){var s=a.isSaving(e);r.set(e,!1),t(),r.set(e,s)}},p=e=>{var i=e,{apply:o}=i;return i.history={undos:[],redos:[]},i.redo=()=>{var{history:e}=i,{redos:s}=e;if(s.length>0){var o=s[s.length-1];a.withoutSaving(i,(()=>{t.withoutNormalizing(i,(()=>{for(var e of o)i.apply(e)}))})),e.redos.pop(),e.undos.push(o)}},i.undo=()=>{var{history:e}=i,{undos:o}=e;if(o.length>0){var r=o[o.length-1];a.withoutSaving(i,(()=>{t.withoutNormalizing(i,(()=>{var e=r.map(s.inverse).reverse();for(var t of e)t===e[e.length-1]&&"set_selection"===t.type&&null==t.newProperties||i.apply(t)}))})),e.redos.push(r),e.undos.pop()}},i.apply=e=>{var{operations:t,history:s}=i,{undos:r}=s,n=r[r.length-1],p=n&&n[n.length-1],d=g(e,p),y=a.isSaving(i),v=a.isMerging(i);if(null==y&&(y=h(e)),y){if(null==v&&(v=null!=n&&(0!==t.length||(l(e,p)||d))),n&&v)d&&n.pop(),n.push(e);else{var f=[e];r.push(f)}for(;r.length>100;)r.shift();u(e)&&(s.redos=[])}o(e)},i},l=(e,t)=>"set_selection"===e.type||(!(!t||"insert_text"!==e.type||"insert_text"!==t.type||e.offset!==t.offset+t.text.length||!i.equals(e.path,t.path))||!(!t||"remove_text"!==e.type||"remove_text"!==t.type||e.offset+e.text.length!==t.offset||!i.equals(e.path,t.path))),h=(e,t)=>"set_selection"!==e.type||null!=e.newProperties,g=(e,t)=>!(!t||"set_selection"!==e.type||"set_selection"!==t.type),u=e=>"set_selection"!==e.type;export{o as History,a as HistoryEditor,n as MERGING,r as SAVING,p as withHistory};
//# sourceMappingURL=index.es.js.map
