import{a as e}from"./rolldown-runtime-CNC7AqOf.js";import{t}from"./react-Augu_vA3.js";import{Dt as n,Si as r,V as i,Vt as a,Wt as o,lt as s,ot as c,si as l,st as u,vn as d,zt as f}from"./forms-CL2ODv4X.js";import{t as p}from"./useTranslation-B_YCgyrR.js";import{t as m}from"./jsx-runtime-CTknu7_t.js";import{n as h}from"./cards-CR_9H_Xm.js";import{n as g}from"./Combobox-BFkUVwV6.js";import{t as _}from"./Modal-C4GWc9Ry.js";import{n as v,t as y}from"./useHasPermission-CRA8mdHC.js";import{t as b}from"./ColorPicker-CDUktmxV.js";import{n as x}from"./useFormatMoney-DFCLiSuN.js";import{t as S}from"./inclusive-tax-N-EV5wzc.js";function C(e){return o([`/api/v1/expense_categories`,e],()=>f(`GET`,n(`/api/v1/expense_categories?per_page=:perPage&page=:currentPage&sort=:sort&filter=:filter&status=:status`,{perPage:e.perPage??`100`,currentPage:e.currentPage??`1`,sort:e.sort??`name|asc`,filter:e.filter??``,status:e.status?.join(`,`)??``})).then(e=>e.data.data),{enabled:e.enabled??!0,staleTime:1/0})}function w(e){return o([`/api/v1/expense_categories`,e.id],()=>f(`GET`,n(`/api/v1/expense_categories/:id`,{id:e.id})),{enabled:e.enabled??!0,staleTime:1/0})}function T(){return(e,t)=>{d.processing(),f(`POST`,n(`/api/v1/expense_categories/bulk`),{action:t,ids:[e]}).then(()=>{d.success(`${t}d_expense_category`),a([`expense_categories`])})}}function E(){let{isAdmin:e}=y();return o(`/api/v1/expense_categories/create`,()=>f(`GET`,n(`/api/v1/expense_categories/create`)).then(e=>e.data.data),{staleTime:1/0,enabled:e})}var D=e(t()),O=e(m(),1);function k(e){let[t]=p(),{errors:n,setErrors:r,setExpenseCategory:a,expenseCategory:o,nameFieldRef:s,withCardContainer:l=!1}=e,u=(e,t)=>{r(void 0),a(n=>n&&{...n,[e]:t})};return l?(0,O.jsxs)(h,{children:[(0,O.jsx)(i,{innerRef:s,required:!0,changeOverride:!0,label:t(`name`),value:o?.name,onValueChange:e=>u(`name`,e),errorMessage:n?.errors.name,cypressRef:`expenseCategoryNameField`}),(0,O.jsxs)(`div`,{children:[(0,O.jsx)(c,{className:`mb-1`,children:t(`color`)}),(0,O.jsx)(b,{value:o?.color,onValueChange:e=>u(`color`,e)})]})]}):(0,O.jsxs)(O.Fragment,{children:[(0,O.jsx)(i,{innerRef:s,required:!0,changeOverride:!0,label:t(`name`),value:o?.name,onValueChange:e=>u(`name`,e),errorMessage:n?.errors.name,cypressRef:`expenseCategoryNameField`}),(0,O.jsxs)(`div`,{children:[(0,O.jsx)(c,{className:`mb-1`,children:t(`color`)}),(0,O.jsx)(b,{value:o?.color,onValueChange:e=>u(`color`,e)})]})]})}function A(e){let[t]=p(),r=s(),{data:i}=E(),[o,c]=(0,D.useState)(),[l,m]=(0,D.useState)(!1),[h,g]=(0,D.useState)();return(0,D.useEffect)(()=>{i&&g({...i,color:r})},[i]),(0,O.jsx)(_,{title:t(`create_expense_category`),visible:e.visible,onClose:()=>e.setVisible(!1),stopPropagationInHeader:!0,children:(0,O.jsxs)(`div`,{className:`flex flex-col space-y-4`,onClick:e=>e.stopPropagation(),children:[(0,O.jsx)(k,{expenseCategory:h,setExpenseCategory:g,errors:o,setErrors:c}),(0,O.jsx)(`div`,{className:`flex justify-end space-x-4 mt-5`,children:(0,O.jsx)(u,{behavior:`button`,onClick:()=>{l||(d.processing(),m(!0),f(`POST`,n(`/api/v1/expense_categories`),h).then(t=>{d.success(`created_expense_category`),a([`expense_categories`]),window.dispatchEvent(new CustomEvent(`invalidate.combobox.queries`,{detail:{url:n(`/api/v1/expense_categories`)}})),e.setSelectedIds&&e.setSelectedIds([t.data.data.id]),e.onCreatedCategory&&e.onCreatedCategory(t.data.data),i&&g({...i,color:r}),e.setVisible(!1)}).catch(e=>{e.response?.status===422&&(c(e.response.data),d.dismiss())}).finally(()=>m(!1)))},children:t(`save`)})})]})})}function j(e){let[t]=p(),[r,i]=(0,D.useState)(!1),a=v();return(0,O.jsxs)(O.Fragment,{children:[(0,O.jsx)(A,{visible:e.initiallyVisible||r,setVisible:e.setVisible||i,setSelectedIds:e.setSelectedIds,onCreatedCategory:t=>e.onChange(t)}),!e.setSelectedIds&&(0,O.jsx)(g,{endpoint:n(`/api/v1/expense_categories?status=active&per_page=500`),onChange:t=>t.resource&&e.onChange(t.resource),inputOptions:{label:e.inputLabel?.toString(),value:e.value||null},entryOptions:{id:`id`,label:`name`,value:`id`},action:{label:t(`new_expense_category`),onClick:()=>i(!0),visible:a(`create_expense`)},readonly:e.readonly,onDismiss:e.onClearButtonClick,initiallyVisible:e.initiallyVisible,sortBy:`name|asc`,staleTime:e.staleTime,errorMessage:e.errorMessage})]})}var M=l(void 0),N=l(void 0),P=(e,t)=>{let n=e<0;return n&&(e*=-1),e=Number(Math.round(Number(e+`e+${t}`))+`e-${t}`),n&&(e*=-1),e};function F(){let e=r(),t=x();return n=>{if(n.uses_inclusive_taxes)return n.amount;if(n.calculate_tax_by_amount)return n.amount+n.tax_amount1+n.tax_amount2+n.tax_amount3;let r=n.currency_id||e?.settings.currency_id,i=t(r)?.precision||2,a=n.amount;if(n.tax_name1){let e=n.amount*(n.tax_rate1/100);a+=P(e,i)}if(n.tax_name2){let e=n.amount*(n.tax_rate2/100);a+=P(e,i)}if(n.tax_name3){let e=n.amount*(n.tax_rate3/100);a+=P(e,i)}return a}}function I(){let e=r(),t=x();return n=>{if(!n.uses_inclusive_taxes)return n.amount;if(n.calculate_tax_by_amount)return n.amount-n.tax_amount1-n.tax_amount2-n.tax_amount3;let r=n.currency_id||e?.settings.currency_id,i=t(r)?.precision||2;return S.backout(n.amount,[n.tax_rate1,n.tax_rate2,n.tax_rate3],i).net}}export{j as a,E as c,w as d,M as i,T as l,I as n,A as o,N as r,k as s,F as t,C as u};