import{CompareType as e}from"../types.js";let getMinValueForInventory=i=>i===e.ABOVE?0:i===e.BELOW?2:1,renderBadgeInFirstTime=(e,i,n)=>`
  {% assign isShow = true %}
  {% assign conditionTriggers = "" %}
  {% assign isInventoryStatus = false %}
  {% assign isDiscountPrice = false %}
  {% assign isPriceRange = false %}
  {% assign isCreationDate = false %}
  {% assign isProductTag = false %}
  ${checkConditionBadge(e)}
  {% assign trigger_arr = conditionTriggers | split: "," %}
  {% for trigger in trigger_arr %}
    {% if trigger == "false" %}
      {% assign isShow = false %}
    {% endif %}
  {% endfor %}
  {% assign price = variant.price | times: 1.0 %}
  {% assign salePrice = variant.compare_at_price | times: 1.0 %}
  {% assign priceSave = salePrice | minus: price %}
  {% assign isEmptyBadgeContent = ${!i} %}
  {% assign isNotDiscount = false %}
  {% assign isBadgeContentHasDiscount = ${n} %}
  {% if isEmptyBadgeContent == true %}
    {% assign isNotDiscount = true %}
  {% endif %}
  {% if isBadgeContentHasDiscount == true and priceSave <= 0 %}
    {% assign isNotDiscount = true %}
  {% endif %}
  {% if isShow and isNotDiscount == false %}
    {% assign className = "" %}
  {% else %}
    {% assign className = "!gp-hidden" %}
  {% endif %}
  `,checkConditionBadge=e=>e?.length?e.map(e=>{switch(e?.triggerEvent){case"discount_price":return checkDiscountPriceCondition(e);case"price_range":return checkPriceRangeCondition(e);case"inventory_status":return checkInventoryStatusCondition(e);case"product_tags":return checkProductTagCondition(e);case"creation_date":return checkCreationDateCondition(e);default:return""}}).join(" "):"",getNumber=e=>e.replace(/[%$]/g,""),compareValueFunc=()=>`
    {% assign enabled = false %}
    {% case conditionType %}
      {% when "exact" %}
        {% if compareValue == from %}
          {% assign enabled = true %}
        {% endif %}
      {% when "above" %}
        {% if compareValue > from %}
          {% assign enabled = true %}
        {% endif %}
      {% when "below" %}
        {% if compareValue < from %}
          {% assign enabled = true %}
        {% endif %}
      {% when "exact_or_above" %}
        {% if compareValue >= from %}
          {% assign enabled = true %}
        {% endif %}
      {% when "exact_or_below" %}
        {% if compareValue <= from %}
          {% assign enabled = true %}
        {% endif %}
      {% when "between" %}
        {% if compareValue >= from and compareValue <= to %}
          {% assign enabled = true %}
        {% endif %}
      {% else %}
        {% assign enabled = false %}
    {% endcase %}
  `,checkDiscountPriceCondition=e=>{let{from:i,to:n,discountType:a,conditionType:s}=e;return`
    {% assign price = variant.price %}
    {% assign compare_at_price = variant.compare_at_price  %}
    {% if price == null or compare_at_price == null or compare_at_price < price %}
      {% assign isDiscountPrice = false %}
    {% else %}
      {% assign from = ${getNumber(i??"0")} %}
      {% assign to = ${getNumber(n??"0")} %}
      {% assign discountType = "${a}" %}
      {% assign conditionType = "${s}" %}
      {% assign price = variant.price | divided_by: 100.0 %}
      {% assign compare_at_price = variant.compare_at_price | divided_by: 100.0 %}
      {% assign discountPrice = compare_at_price | minus: price %}
      {% assign discountByPercentage = discountPrice | divided_by: compare_at_price | times: 100.0 | round %}
      {% assign compareValue = discountPrice %}
      {% if discountType == "percent" %}
        {% assign compareValue = discountByPercentage %}
      {% endif %}
      ${compareValueFunc()}
      {% assign isDiscountPrice = enabled %}
    {% endif %}
    {% assign conditionTriggers = conditionTriggers | append: "," | append: isDiscountPrice %}
  `},checkPriceRangeCondition=e=>{let{from:i,to:n,conditionType:a}=e;return`
    {% assign price = variant.price %}
    {% if price == null %}
      {% assign isPriceRange = false %}
    {% else %}
      {% assign from = ${getNumber(i??"0")} %}
      {% assign to = ${getNumber(n??"0")} %}
      {% assign conditionType = "${a}" %}
      {% assign compareValue = variant.price | divided_by: 100.0 %}
      ${compareValueFunc()}
      {% assign isPriceRange = enabled %}
    {% endif %}
    {% assign conditionTriggers = conditionTriggers | append: "," | append: isPriceRange %}
  `},checkInventoryStatusCondition=e=>{let{type:i,conditionType:n,from:a,to:s}=e,r=getMinValueForInventory(n);return`
      {% assign type = "${i}" %}
      {% assign shouldContinue = false %}
      {% if variant.inventory_policy == "continue" and type != "out_of_stock" %}
        {% assign shouldContinue = true %}
      {% endif %}
      {% if variant.inventory_management == null or shouldContinue  %}
        {% assign conditionTriggers = conditionTriggers | append: "," | append: true %}
      {% elsif variant.inventory_quantity != null %}
        {% assign compareValue = variant.inventory_quantity %}
        {% if type == "in_stock" %}
          {% assign conditionType = "${n}" %}
          {% assign from = ${getNumber(a)} %}
          {% assign to = ${getNumber(s??"0")} %}
          ${compareValueFunc()}
          {% if conditionType == "below" or conditionType == "exact_or_below" %}
            {% assign min = ${r} %}
            {% assign from = 1 %}
            {% assign to = from | minus: min | plus: 1 %}
            {% if compareValue >= from and compareValue <= to %}
              {% assign enabled = true %}
            {% else %}
              {% assign enabled = false %}
            {% endif %}
          {% endif %}
          {% assign isInventoryStatus = enabled %}
        {% else %}
          {% if compareValue <= 0 and variant.inventory_policy != "continue"  %}
            {% assign isInventoryStatus = true %}
          {% endif %}
        {% endif %}
        {% assign conditionTriggers = conditionTriggers | append: "," | append: isInventoryStatus %}
      {% else %}
        {% assign conditionTriggers = conditionTriggers | append: "," | append: false %}
      {% endif %}

  `},checkProductTagCondition=e=>{let{searchTag:i}=e,n=i.map(e=>e.replaceAll("'","&#039;")).join(", ");return`
    {% assign productTags = product.tags | escape %}
    {% assign searchTag = '${n}' %}
    {% for tag in productTags %}
      {% if searchTag contains tag %}
        {% assign isProductTag = true %}
        {% break %}
      {% endif %}
    {% endfor %}
    {% assign conditionTriggers = conditionTriggers | append: "," | append: isProductTag %}
  `},checkCreationDateCondition=e=>{let{days:i,from:n,to:a,type:s,conditionType:r}=e;return`
    {% assign type = "${s}" %}
    {% assign days = ${getNumber(i)} %}
    {% assign from = "${n}" | date: "%Y-%m-%d" | date: "%s" | times: 1000  %}
    {% assign currentTime = "now" | date: "%Y-%m-%d" | date: "%s" | times: 1000 %}
    {% assign compareValue = product.created_at | date: "%Y-%m-%d" | date: "%s" | times: 1000 %}
    {% if type == "exact_date" %}
      {% if compareValue == from %}
        {% assign enabled = true %}
      {% else %}
        {% assign enabled = false %}
      {% endif %}
    {% elsif type == "duration" %}
      {% assign timeDifference = currentTime | minus: compareValue  %}
      {% assign dayDifference = timeDifference | divided_by: 86400000 %}
      {% if dayDifference >= 0 and dayDifference <= days %}
        {% assign enabled = true %}
      {% else %}
        {% assign enabled = false %}
      {% endif %}
    {% else %}
      {% assign to = "${a}" | date: "%Y-%m-%d" | date: "%s" | times: 1000 %}
      {% assign conditionType = "${r}" %}
      {% if from >= to %}
        {% assign enabled = false %}
      {% else %}
        ${compareValueFunc()}
      {% endif %}
    {% endif %}
    {% assign isCreationDate = enabled %}
    {% assign conditionTriggers = conditionTriggers | append: "," | append: isCreationDate %}
  `};export{renderBadgeInFirstTime};
