import{t as e}from"./db-D2Po16Ap.js";function t(){return crypto.randomUUID?.()??`_${Date.now()}_${Math.random().toString(36).slice(2)}`}async function n(){return e.foods.orderBy(`createdAt`).toArray()}async function r(n){let r=new Date().toISOString(),i=Math.max(0,Math.round(n.proteinPer100*10)/10),a=n.proteinSource,o=typeof n.qualityProteinPer100==`number`?Math.max(0,Math.round(n.qualityProteinPer100*10)/10):a===`animal`?i:a===`plant`?0:void 0,s={id:t(),name:n.name.trim()||`Продукт`,kcalPer100:Math.max(0,Math.round(n.kcalPer100)),proteinPer100:i,fatPer100:Math.max(0,Math.round(n.fatPer100*10)/10),carbsPer100:Math.max(0,Math.round(n.carbsPer100*10)/10),...n.catalogId?{catalogId:n.catalogId}:{},...a?{proteinSource:a}:{},...o==null?{}:{qualityProteinPer100:o},...n.hasFastCarbs===!0?{hasFastCarbs:!0}:{},createdAt:r,updatedAt:r};return await e.foods.add(s),s}async function i(t,n){let r=await e.foods.get(t);if(!r)return;let i={...r,...`name`in n?{name:(n.name??r.name).trim()||r.name}:{},...n.kcalPer100==null?{}:{kcalPer100:Math.max(0,Math.round(n.kcalPer100))},...n.proteinPer100==null?{}:{proteinPer100:Math.max(0,Math.round(n.proteinPer100*10)/10)},...n.fatPer100==null?{}:{fatPer100:Math.max(0,Math.round(n.fatPer100*10)/10)},...n.carbsPer100==null?{}:{carbsPer100:Math.max(0,Math.round(n.carbsPer100*10)/10)},...n.catalogId===void 0?{}:{catalogId:n.catalogId},...n.proteinSource===void 0?{}:{proteinSource:n.proteinSource},...n.qualityProteinPer100===void 0?{}:{qualityProteinPer100:n.qualityProteinPer100==null?void 0:Math.max(0,Math.round(n.qualityProteinPer100*10)/10)},...n.hasFastCarbs===void 0?{}:{hasFastCarbs:n.hasFastCarbs},updatedAt:new Date().toISOString()};return await e.foods.put(i),i}async function a(t){await e.transaction(`rw`,e.foods,e.mealEntries,async()=>{await e.mealEntries.where(`foodId`).equals(t).delete(),await e.foods.delete(t)})}async function o(n){let r=await e.foods.get(n.foodId);if(!r)return null;let i=Math.max(1,Math.round(n.grams)),a=i/100,o=Math.round(r.kcalPer100*a),s=Math.round(r.proteinPer100*a*10)/10,c=Math.round(r.fatPer100*a*10)/10,l=Math.round(r.carbsPer100*a*10)/10,u=new Date().toISOString(),d={id:t(),dateISO:n.dateISO,timeHHMM:n.timeHHMM,foodId:n.foodId,grams:i,kcal:o,protein:s,fat:c,carbs:l,createdAt:u,...n.mealGroupId==null?{}:{mealGroupId:n.mealGroupId},...n.recipeId==null?{}:{recipeId:n.recipeId},...n.recipeName==null?{}:{recipeName:n.recipeName},...n.isRecipeIngredient===!0?{isRecipeIngredient:!0}:{},...n.sourcePlanWeekStartISO==null?{}:{sourcePlanWeekStartISO:n.sourcePlanWeekStartISO},...n.sourcePlanDateISO==null?{}:{sourcePlanDateISO:n.sourcePlanDateISO},...n.sourcePlanMealIndex==null?{}:{sourcePlanMealIndex:n.sourcePlanMealIndex},...n.sourcePlanRecipeId==null?{}:{sourcePlanRecipeId:n.sourcePlanRecipeId}};return await e.mealEntries.add(d),d}async function s(t){await e.mealEntries.delete(t)}async function c(t){await e.mealEntries.where(`mealGroupId`).equals(t).delete()}async function l(t){let n=(await e.mealEntries.where(`dateISO`).equals(t).toArray()).filter(e=>e.sourcePlanDateISO===t);await Promise.all(n.map(t=>e.mealEntries.delete(t.id)))}async function u(t){let[n,r]=await Promise.all([e.mealEntries.where(`dateISO`).equals(t).toArray(),e.foods.toArray()]),i=new Map(r.map(e=>[e.id,e.name]));return n.slice().sort((e,t)=>e.timeHHMM>t.timeHHMM?1:e.timeHHMM({...e,foodName:i.get(e.foodId)??`Продукт`}))}async function d(t){let[n,r]=await Promise.all([e.mealEntries.where(`dateISO`).equals(t).toArray(),e.foods.toArray()]),i=new Map(r.map(e=>[e.id,e])),a=0,o=0,s=0,c=0,l=0,u=0;for(let e of n){a+=e.kcal,o+=e.protein,c+=e.fat,l+=e.carbs;let t=i.get(e.foodId);if(t){let n=e.grams/100,r=typeof t.qualityProteinPer100==`number`?t.qualityProteinPer100:t.proteinPer100,i=Math.max(0,Math.round(r*n*10)/10);s+=i}else s+=e.protein;e.timeHHMM<`18:00`&&(u+=e.carbs)}let d=Math.max(0,l-u);return{kcal:a,protein:o,proteinQuality:s,fat:c,carbs:l,carbsBefore18:u,carbsAfter18:d}}async function f(t){let[n,r]=await Promise.all([e.mealEntries.where(`dateISO`).equals(t).toArray(),e.foods.toArray()]),i=new Map(r.map(e=>[e.id,e])),a=n.filter(e=>!!e.mealGroupId&&!!e.recipeId&&!!e.recipeName),o=new Map;for(let e of a){let t=o.get(e.mealGroupId)??[];t.push(e),o.set(e.mealGroupId,t)}let s=[];for(let[,e]of o){let t=e[0],n=e.map(e=>{let t=i.get(e.foodId);return{foodId:e.foodId,grams:e.grams,catalogId:t?.catalogId}});s.push({timeHHMM:t.timeHHMM,recipeId:t.recipeId,recipeName:t.recipeName,mealGroupId:t.mealGroupId,ingredients:n})}return s.sort((e,t)=>e.timeHHMM>t.timeHHMM?1:e.timeHHMM!!e.catalogId),r=[...new Set(n.map(e=>e.catalogId))],i=0,a=0;for(let e of r){let n=t[e];if(!n){a++;continue}let r=await g(e,m(n));i+=r}return{updated:i,missing:a}}export{p as a,l as c,u as d,d as f,g,i as h,r as i,s as l,m,_ as n,a as o,f as p,h as r,c as s,o as t,n as u};