"use strict";var types=require("../types.js");const 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||[],a={...t,product_created_at:o,product_tags:n},i=e?.map(e=>{switch(e?.triggerEvent){case"inventory_status":return inventoryTrigger(a,e);case"discount_price":return discountPriceTrigger(a,e);case"price_range":return priceRangeTrigger(a,e);case"creation_date":return creationDateTrigger(a,e);case"product_tags":return productTagsTrigger(a,e);default:return!1}});return i?.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=e=>e===types.CompareType.ABOVE?0:e===types.CompareType.BELOW?2:1,inventoryTrigger=(e,r)=>{if(void 0===e.inventoryQuantity)return!1;if("in_stock"===r.type){if(!1===e.manageInventory||e.inventoryPolicy?.toLowerCase()=="continue")return!0;if(r.conditionType===types.CompareType.BELOW||r.conditionType===types.CompareType.EXACT_OR_BELOW){let t=getMinValueForInventory(r.conditionType);return compareNumberFunc(e?.inventoryQuantity,{from:1,to:Number(r.from)-t+1},types.CompareType.BETWEEN)}return compareNumberFunc(e?.inventoryQuantity,{from:Number(r.from),to:Number(r?.to)},r.conditionType)}return!1!==e.manageInventory&&e.inventoryPolicy?.toLowerCase()!="continue"&&compareNumberFunc(e.inventoryQuantity,{from:0,to:0},types.CompareType.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=(e,r)=>{if(!e.product_created_at)return!1;let t=new Date(new Date(e.product_created_at).setHours(0,0,0,0)),o=new Date(r.from),n=new Date(r.to),a=t.getTime();if("exact_date"===r.type)return compareNumberFunc(a,{from:o.getTime(),to:0},types.CompareType.EXACT);if("duration"===r.type){let e=new Date().setHours(0,0,0,0),o=(e-t.getTime())/864e5;if(o>=0&&o<=Number(r.days))return!0}return!(o.getTime()>=n.getTime())&&compareNumberFunc(a,{from:o.getTime(),to:n.getTime()},types.CompareType.BETWEEN)},productTagsTrigger=(e,r)=>{let t=e.product_tags||[],o=r.searchTag;return o.some(e=>t.includes(e))};exports.checkShowBadge=checkShowBadge,exports.toggleBadge=toggleBadge;
