UNPKG

4.36 kBJavaScriptView Raw
1import{objectSpread2 as e,typeof as n,slicedToArray as t,defineProperty as r,asyncToGenerator as a,regeneratorRuntime as o}from"./_virtual/_rollupPluginBabelHelpers.js";import i from"react";import{deepClone as c,incDec as u,remove as s,setArray as l,set as f,empty as d,format as p,pick as y}from"./utils/index.js";var v=function(e){if(Array.isArray(e))return[];var n;return null!==(n={string:"",number:0}[e])&&void 0!==n?n:""},b=function(n,r){switch(r.type){case"SET":for(var a=0,o=Object.entries(r.payload);a<o.length;a++){var i=t(o[a],2),c=i[0],d=i[1];f(n,d,c)}return e({},n);case"AR":var p=r.payload,y=p.key,v=p.data,b=p.position;return l(n,v,y,b),e({},n);case"RA":var m=r.payload,g=m.key,j=m.condition;return s(n,g,j),e({},n);case"II":for(var k=0,h=Object.entries(r.payload);k<h.length;k++){var A=t(h[k],2),O=A[0],S=A[1];u(n,S,O,"inc")}return e({},n);case"DI":for(var I=0,V=Object.entries(r.payload);I<V.length;I++){var x=t(V[I],2),R=x[0],w=x[1];u(n,w,R,"dec")}return e({},n);default:return e({},n)}},m=function(u){var s=new AbortController,l=e({defaultValue:{}},u),f="object"===n(l.defaultValue)?c(l.defaultValue):l.defaultValue,m=i.useState(void 0),g=t(m,2),j=g[0],k=g[1],h=i.useReducer(b,f),A=t(h,2),O=A[0],S=A[1],I=i.useState(!1),V=t(I,2),x=V[0],R=V[1],w=i.useMemo((function(){return Object.entries(O).filter((function(e){var n=t(e,1)[0];return!Object.keys(l.defaultValue).includes(n)}))}),[O,l.defaultValue]),E=function(e){if(l.format&&!d(l.format))for(var n=0,r=Object.entries(e);n<r.length;n++){var a=t(r[n],2),o=a[0],i=a[1];p(e,l.format,i,o,O)}S({type:"SET",payload:e})},C=i.useCallback((function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"start";S({type:"AR",payload:{key:e,data:t instanceof File?t:"object"===n(t)?Object.assign({},t):t,position:r}})}),[O]),T=i.useCallback((function(e,t){var a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"start",i="function"==typeof t?t({}):t,c=i instanceof File?i:"object"===n(i)?Object.assign({},i):i,u=e.split(".").reduce((function(e,n){var t=e[n];return t||[]}),O),s=-1;-1===(s=i instanceof File&&a.length||"object"===n(i)&&a.length?(u||[]).findIndex((function(e){return a.every((function(n){return e[n]===c[n]}))})):(u||[]).findIndex((function(e){return e===i})))?S({type:"AR",payload:{key:e,data:c,position:o}}):E(r({},"".concat(e,".").concat(s),"function"==typeof t?t(u[s]):c))}),[O]),D=i.useCallback((function(e){S({type:"RA",payload:{key:e,condition:arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0}})}),[O]),F=function(){var n=a(o().mark((function n(t){var r,a,i;return o().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return r=e({scenario:j,service:function(e){},onSuccess:function(e){},onError:function(e){},onAlways:function(){}},t),n.prev=1,R(!0),a={ctr:s,scenario:r.scenario,data:B,keys:_},n.next=6,r.service(a);case 6:i=n.sent,r.onSuccess(i),n.next=13;break;case 10:n.prev=10,n.t0=n.catch(1),r.onError(n.t0);case 13:return n.prev=13,R(!1),r.onAlways(),n.finish(13);case 17:case"end":return n.stop()}}),n,null,[[1,10,13,17]])})));return function(e){return n.apply(this,arguments)}}(),P=i.useCallback((function(e,t){var r=e.split(".").reduce((function(e,n){var t=e[n];return t||""}),O);return r||(Array.isArray(t)||"boolean"==typeof t||"number"==typeof t||"object"===n(t)||"function"==typeof t?t:t||"")}),[O]),_=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:void 0;return e&&!u.scenario?[]:"string"==typeof e&&e in u.scenario?[{name:e,keys:u.scenario[e]}]:"boolean"==typeof e&&e?Object.entries(u.scenario).map((function(e){var n=t(e,2);return{name:n[0],keys:n[1]}})):[{name:"default",keys:Object.keys(O||{})}]},B=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:void 0;return"boolean"==typeof e&&e?O:"string"==typeof e&&null!=u&&u.scenario&&e in u.scenario?y(O,u.scenario[e]||[]):O};return{scenario:j,processing:x,data:B,cancel:function(){s.abort()},setData:E,increment:function(e){S({type:"II",payload:e})},decrement:function(e){S({type:"DI",payload:e})},add:C,remove:D,value:P,reset:function(){Object.assign(l.defaultValue,Object.fromEntries(w.map((function(e){var n=t(e,2),r=n[0],a=n[1];return[r,v(a)]})))),S({type:"SET",payload:l.defaultValue})},send:F,keys:_,setScenario:function(e){k(e)},upsert:T}};export{m as default};