import{CompareType as e}from"../types.js";let toggleBadge=(e,r,t)=>{let o=checkShowBadge(e,r,t);return o?"":"!gp-hidden"},checkShowBadge=(e,r,t)=>{if(!t)return;let o=r?.createdAt||"",n=r?.tags||[],i={...t,product_created_at:o,product_tags:n},c=e?.map(e=>{switch(e?.triggerEvent){case"inventory_status":return inventoryTrigger(i,e);case"discount_price":return discountPriceTrigger(i,e);case"price_range":return priceRangeTrigger(i,e);case"creation_date":return creationDateTrigger(i,e);case"product_tags":return productTagsTrigger(i,e);default:return!1}});return c?.every(e=>!0===e)},compareNumberFunc=(e,r,t)=>{switch(t){case"exact":return e===r.from;case"above":return e>r.from;case"below":return e<r.from;case"exact_or_above":return e>=r.from;case"exact_or_below":return e<=r.from;case"between":return e>=r.from&&e<=r.to;default:return!1}},getMinValueForInventory=r=>r===e.ABOVE?0:r===e.BELOW?2:1,inventoryTrigger=(r,t)=>{if(void 0===r.inventoryQuantity)return!1;if("in_stock"===t.type){if(!1===r.manageInventory||r.inventoryPolicy?.toLowerCase()=="continue")return!0;if(t.conditionType===e.BELOW||t.conditionType===e.EXACT_OR_BELOW){let o=getMinValueForInventory(t.conditionType);return compareNumberFunc(r?.inventoryQuantity,{from:1,to:Number(t.from)-o+1},e.BETWEEN)}return compareNumberFunc(r?.inventoryQuantity,{from:Number(t.from),to:Number(t?.to)},t.conditionType)}return!1!==r.manageInventory&&r.inventoryPolicy?.toLowerCase()!="continue"&&compareNumberFunc(r.inventoryQuantity,{from:0,to:0},e.EXACT_OR_BELOW)},discountPriceTrigger=(e,r)=>{if(e?.compare_at_price===void 0||void 0===e.price||e.compare_at_price<e.price)return!1;let t=(e.compare_at_price-e.price)/100,o=t/(e.compare_at_price/100),n="fixed"===r.discountType?t:Math.round(100*o);return compareNumberFunc(n,{from:Number(r.from),to:Number(r.to)},r.conditionType)},priceRangeTrigger=(e,r)=>{if(void 0===e.price)return!1;let t=e.price/100;return compareNumberFunc(t,{from:Number(r.from),to:Number(r.to)},r.conditionType)},creationDateTrigger=(r,t)=>{if(!r.product_created_at)return!1;let o=new Date(new Date(r.product_created_at).setHours(0,0,0,0)),n=new Date(t.from),i=new Date(t.to),c=o.getTime();if("exact_date"===t.type)return compareNumberFunc(c,{from:n.getTime(),to:0},e.EXACT);if("duration"===t.type){let e=new Date().setHours(0,0,0,0),r=(e-o.getTime())/864e5;if(r>=0&&r<=Number(t.days))return!0}return!(n.getTime()>=i.getTime())&&compareNumberFunc(c,{from:n.getTime(),to:i.getTime()},e.BETWEEN)},productTagsTrigger=(e,r)=>{let t=e.product_tags||[],o=r.searchTag;return o.some(e=>t.includes(e))};export{checkShowBadge,toggleBadge};
