import{t as e}from"./db-D2Po16Ap.js";import{B as t,L as n}from"./Card-Cxq4Ee-X.js";import{B as r}from"./supplementPlanReview-ClVU7EUk.js";var i=[`capsule`,`tablet`,`softgel`,`powder`,`liquid`,`gummy`,`sachet`,`other`],a=[`capsule`,`tablet`,`softgel`,`scoop`,`g`,`mg`,`mcg`,`iu`,`ml`,`serving`,`drop`,`bottle`,`other`];function o(e,r){return t(`supplements.messages.dbValidation.${e}`,r,n())}function s(e){return crypto.randomUUID?.()??`${e}_${Date.now()}_${Math.random().toString(36).slice(2)}`}function c(){return new Date().toISOString()}function l(e){return e!=null&&Number.isFinite(e)&&e>0}function u(e){return e!=null&&Number.isFinite(e)&&e>=0}function d(e){return Number.isFinite(Date.parse(e))}function f(e){return e?.trim()||void 0}function p(e,t){let n=t.updatedAtISO.localeCompare(e.updatedAtISO);return n===0?e.name.localeCompare(t.name,`ru`):n}function m(e,t){let n=t.updatedAtISO.localeCompare(e.updatedAtISO);return n===0?e.id.localeCompare(t.id):n}function h(e){if(!i.includes(e))throw Error(o(`invalidProductForm`,{form:e}))}function g(e,t){if(!e.trim())throw Error(o(`fieldRequired`,{fieldName:t}));if(!a.includes(e))throw Error(o(`invalidUnit`,{fieldName:t,unit:e}));return e}function _(e,t){if(e==null)return;let n=e.trim();if(n){if(!d(n))throw Error(o(`invalidIsoDate`,{fieldName:t}));return n}}function v(e){let t=e.name?.trim();if(!t)throw Error(o(`ingredientNameRequired`));if(!l(e.amount))throw Error(o(`ingredientAmountPositive`));let n=String(e.unit).trim();if(!n)throw Error(o(`ingredientUnitRequired`));let r=g(String(e.per),`ingredient.per`);return{...e,name:t,normalizedName:f(e.normalizedName),amount:e.amount,unit:n,per:r,mapsToSupplementKnowledgeId:f(e.mapsToSupplementKnowledgeId),notes:f(e.notes)}}function y(e){if(e){if(!l(e.amount))throw Error(o(`labelServingSizeAmountPositive`));return{amount:e.amount,unit:g(String(e.unit),`labelServingSize.unit`),labelText:f(e.labelText)}}}function b(e){if(e){if(!l(e.totalUnits))throw Error(o(`packageQuantityTotalUnitsPositive`));return{totalUnits:e.totalUnits,unit:g(String(e.unit),`packageQuantity.unit`),servingsPerPackage:l(e.servingsPerPackage)?e.servingsPerPackage:void 0,labelText:f(e.labelText)}}}function x(e){if(e){if(e.servingsTotal!=null&&!u(e.servingsTotal))throw Error(o(`inventoryServingsTotalNonNegative`));if(e.servingsRemaining!=null&&!u(e.servingsRemaining))throw Error(o(`inventoryServingsRemainingNonNegative`));if(e.unitsRemaining!=null&&!u(e.unitsRemaining))throw Error(o(`inventoryUnitsRemainingNonNegative`));if(e.lowStockThresholdServings!=null&&!u(e.lowStockThresholdServings))throw Error(o(`inventoryLowStockThresholdNonNegative`));return{servingsTotal:e.servingsTotal,servingsRemaining:e.servingsRemaining,unitsRemaining:e.unitsRemaining,lowStockThresholdServings:e.lowStockThresholdServings,lastCountedAtISO:_(e.lastCountedAtISO,`inventory.lastCountedAtISO`)}}}function S(e){let t=e??`manual`;if(![`manual`,`import_backup`,`ocr_draft`,`barcode_lookup`,`telegram`].includes(t))throw Error(o(`invalidProductSource`,{value:t}));return t}function C(e,t){let n=t?.nowISO??c(),r=e.name?.trim();if(!r)throw Error(o(`productNameRequired`));h(e.form);let i=e.purchasePrice;if(i!=null&&!u(i))throw Error(o(`purchasePriceNonNegative`));let a=(e.activeIngredients??[]).map(v),l=t?.existingCreatedAtISO??_(e.createdAtISO,`createdAtISO`)??n;return{id:`id`in e&&e.id?e.id:s(`supp_product`),supplementKnowledgeId:f(e.supplementKnowledgeId),supplementCatalogId:f(e.supplementCatalogId),name:r,brand:f(e.brand),form:e.form,labelServingSize:y(e.labelServingSize),activeIngredients:a,packageQuantity:b(e.packageQuantity),inventory:x(e.inventory),openedAtISO:_(e.openedAtISO,`openedAtISO`),expiresAtISO:_(e.expiresAtISO,`expiresAtISO`),purchasePrice:i??void 0,currency:f(e.currency)?.toUpperCase(),purchaseDateISO:_(e.purchaseDateISO,`purchaseDateISO`),source:S(e.source),notes:f(e.notes),archivedAtISO:_(e.archivedAtISO,`archivedAtISO`),createdAtISO:l,updatedAtISO:n}}function w(e,t){return t?.includeArchived?e:e.filter(e=>!r(e))}function T(e,t){let n=t?.nowISO??c(),r=e.planItemId?.trim();if(!r)throw Error(o(`planItemIdRequired`));let i=e.productId?.trim();if(!i)throw Error(o(`productIdRequired`));if(e.servingsPerPlanDose!=null&&!l(e.servingsPerPlanDose))throw Error(o(`servingsPerPlanDosePositive`));if(e.unitsPerPlanDose!=null&&!l(e.unitsPerPlanDose))throw Error(o(`unitsPerPlanDosePositive`));let a=t?.existingCreatedAtISO??_(e.createdAtISO,`createdAtISO`)??n;return{id:`id`in e&&e.id?e.id:s(`supp_product_link`),planItemId:r,productId:i,servingsPerPlanDose:e.servingsPerPlanDose,unitsPerPlanDose:e.unitsPerPlanDose,isActive:e.isActive??!0,createdAtISO:a,updatedAtISO:n}}async function E(t,n){let r=C(t,n);return await e.supplementProducts.add(r),r}async function D(t){return e.supplementProducts.get(t)}async function O(t){return w(await e.supplementProducts.toArray(),t).sort(p)}async function k(t,n,r){let i=await e.supplementProducts.get(t);if(!i)throw Error(o(`productNotFound`,{id:t}));let a=C({...i,...n,id:t,activeIngredients:n.activeIngredients??i.activeIngredients},{...r,existingCreatedAtISO:i.createdAtISO});return a.id=t,await e.supplementProducts.put(a),a}async function A(t,n){let r=await e.supplementPlanProductLinks.where(`productId`).equals(t).toArray(),i=0;for(let t of r)t.isActive&&(await e.supplementPlanProductLinks.put({...t,isActive:!1,updatedAtISO:n}),i++);return i}async function j(t,n){let r=n?.nowISO??c();return e.transaction(`rw`,[e.supplementProducts,e.supplementPlanProductLinks],async()=>{let n=await e.supplementProducts.get(t);if(!n)throw Error(o(`productNotFound`,{id:t}));await A(t,r);let i={...n,archivedAtISO:n.archivedAtISO??r,updatedAtISO:r};return await e.supplementProducts.put(i),i})}async function M(t,n){let r=n?.nowISO??c(),i=await e.supplementProducts.get(t);if(!i)throw Error(o(`productNotFound`,{id:t}));let a={...i,archivedAtISO:void 0,updatedAtISO:r};return await e.supplementProducts.put(a),a}async function N(t){await e.transaction(`rw`,[e.supplementProducts,e.supplementPlanProductLinks],async()=>{let n=await e.supplementPlanProductLinks.where(`productId`).equals(t).toArray();await Promise.all(n.map(t=>e.supplementPlanProductLinks.delete(t.id))),await e.supplementProducts.delete(t)})}async function P(t,n){let r=T(t,n);return await e.supplementPlanProductLinks.add(r),r}async function F(){return(await e.supplementPlanProductLinks.toArray()).sort(m)}async function I(t,n){let r=await e.supplementPlanProductLinks.get(t);if(!r)return;let i=n?.nowISO??c(),a={...r,isActive:!1,updatedAtISO:i};return await e.supplementPlanProductLinks.put(a),a}async function L(t,n){let i=n?.nowISO??c(),a=t.planItemId.trim(),s=await e.supplementProducts.get(t.productId);if(!s)throw Error(o(`productNotFound`,{id:t.productId}));if(r(s))throw Error(o(`archivedProductLinkBlocked`));let l=(await e.supplementPlanProductLinks.where(`planItemId`).equals(a).toArray()).filter(e=>e.isActive);for(let t of l)await e.supplementPlanProductLinks.put({...t,isActive:!1,updatedAtISO:i});return P({planItemId:t.planItemId,productId:t.productId,servingsPerPlanDose:t.servingsPerPlanDose,unitsPerPlanDose:t.unitsPerPlanDose,isActive:!0},{nowISO:i})}export{N as a,O as c,M as d,i as f,D as i,C as l,E as n,L as o,a as p,I as r,F as s,j as t,k as u};