import{n as e}from"./dist-yS4fzweZ.js";var t=new Set;function n(e){return t.add(e),()=>t.delete(e)}function r(e){t.forEach(t=>t(e))}var i=new Set;function a(e){return i.add(e),()=>i.delete(e)}function o(e,t){i.forEach(n=>n(e,t))}function s(){i.forEach(e=>e([],``))}var c=new Set([`error`,`errors`,`detail`,`message`,`code`,`type`,`status`,`non_field_errors`,`nonFieldErrors`]);function l(e){if(e==null)return``;if(typeof e==`string`)return e;if(typeof e==`object`&&e.string)return String(e.string);if(typeof e==`object`&&e.message)return String(e.message);if(Array.isArray(e))return e.map(l).filter(Boolean).join(`, `);if(typeof e==`object`){let t=Object.values(e).map(u).filter(Boolean);if(t.length)return t.join(`; `)}return typeof e.toString==`function`&&e.toString()!==`[object Object]`?e.toString():String(e)}function u(e){return Array.isArray(e)?e.map(l).filter(Boolean).join(`, `):l(e)}function d(e){let t=Object.keys(e);if(t.length===0)return!1;let n=t.filter(e=>!c.has(e));return n.length>0&&n.length>=t.length*.5}function f(e){if(e.errors&&typeof e.errors==`object`){let t=[];if(Object.entries(e.errors).forEach(([e,n])=>{let r=Array.isArray(n)?n.join(`, `):String(n);r&&t.push({field:e,message:r})}),t.length>0)return t}let t=e.response?.data||e.data||{},n=[];if(!t||typeof t!=`object`)return e.message?[{field:`error`,message:e.message}]:[];if(t.error?.details&&typeof t.error.details==`object`)return Object.entries(t.error.details).forEach(([e,t])=>{n.push({field:e,message:u(t)})}),n;if(t.errors||t.error&&typeof t.error==`object`){let e=t.errors||t.error;if(e.details&&typeof e.details==`object`)return Object.entries(e.details).forEach(([e,t])=>{n.push({field:e,message:u(t)})}),n;if(e.message)return[{field:`error`,message:String(e.message)}];if(typeof e==`object`&&!Array.isArray(e)&&(Object.entries(e).forEach(([e,t])=>{[`code`,`message`,`details`].includes(e)||n.push({field:e,message:u(t)})}),n.length>0))return n}if(typeof t.error==`string`)return[{field:`error`,message:t.error}];if(t.non_field_errors){let e=Array.isArray(t.non_field_errors)?t.non_field_errors.map(l).filter(Boolean):[l(t.non_field_errors)];if(e.length>0)return[{field:`error`,message:e.join(` `)}]}return t.detail?[{field:`error`,message:String(t.detail)}]:t.message?[{field:`error`,message:String(t.message)}]:d(t)?(Object.entries(t).forEach(([e,t])=>{n.push({field:e,message:u(t)})}),n):e.message?[{field:`error`,message:e.message}]:n}function p(e){return e?.response?.data?.error?.code||e?.data?.error?.code||null}function m(e){let t=e?.response?.data?.error?.message||e?.data?.error?.message||e?.message||``,n=t.match(/Cannot post on (\d{4}-\d{2}-\d{2})/),r=t.match(/'([^']+)'/);return{type:`PERIOD_LOCKED`,attemptedDate:n?.[1]??``,periodName:r?.[1]??`this period`,message:t}}function h(e){let t=e?.response?.data?.error?.message||e?.data?.error?.message||e?.message||``,n=t.match(/Cannot post on (\d{4}-\d{2}-\d{2})/),r=t.match(/on or before (\d{4}-\d{2}-\d{2})/);return{type:`TRANSACTION_FROZEN`,attemptedDate:n?.[1]??``,frozenUntil:r?.[1]??``,message:t}}function g(t,n=`An error occurred`,i){let{showModal:a=!0}=i??{},s=p(t);if(s===`PERIOD_LOCKED`)return r(m(t)),[];if(s===`TRANSACTION_FROZEN`)return r(h(t)),[];let c=f(t);if(c.length===0)return e.error(n,{duration:5e3}),[];let l=c.filter(e=>e.field===`error`),u=c.filter(e=>e.field!==`error`);if(l.forEach(({message:t})=>{e.error(t,{duration:5e3})}),l.length===0&&u.length===0&&e.error(n,{duration:5e3}),u.length>0){if(l.length===0){let t=u[0],r=t.field.replace(/_id$/,``).replace(/_/g,` `),i=u.length>1?`${r}: ${t.message} (+${u.length-1} more)`:`${r}: ${t.message}`;e.error(n,{description:i,duration:7e3})}a&&o(u,n)}return c}export{n as i,g as n,a as r,s as t};