@use "sass:map";
@use "@twig/scss/_global" as global;
@use "@twig/scss/variables/_variables-components" as components;

$params: (
   "item-gap": 10px,
   "item-gap-lg": 25px,
   "item-padding": 0 0 global.$gap-width 0,
   "item-padding-lg": 0 (global.$gap-width * 2) global.$gap-width 0,
   "item-border-bottom": global.$border-width solid global.$border-color,
   "item-line-height": global.$line-height-base,

   "image-overlay": true,
   "image-overlay-padding": global.$gap-width,
   "image-width": 80px,
   "image-width-lg": 115px,
   "image-width-price-beside-title": 72px,
   "image-border": 0,
   "image-border-radius": global.$border-radius-lg,

   "title-font-size": global.$font-size-base,
   "title-font-weight": global.$font-weight-base,
   "title-color": global.$text-dark,
   "title-margin": 0 0 8px 0,
   "title-line-clamp": 2,

   "price-margin": 0,
   "price-font-size": 18px,
   "price-font-weight": global.$font-weight-bold,
   "original-price-color": global.$text-muted,
   "original-price-font-size": global.$font-size-smaller,

   "price-beside-title-width": 120px,

   "delivery-font-size": global.$font-size-smaller,
   "delivery-font-weight": global.$font-weight-base,

   "button-max-width": 240px,
   "button-no-text-size": 42px,
   "button-no-text-icon-size": 20px,

   "addtocart-padding-right": global.$btn-padding-x,
   "addtocart-padding-left": global.$btn-padding-x,
   "addtocart-padding-right-lg": global.$btn-padding-x,
   "addtocart-padding-left-lg": global.$btn-padding-x,
);

@if global-variable-exists(c-productlist-lineitem, components) {
   $keys: map.keys(components.$c-productlist-lineitem);
   @each $name in $keys {
      @if not map.get($params, $name) {
         @warn "Neexistující proměnná '#{$name}' v komponentě '$#{component("ProductList:LineItem", "class")}'.";
      }
   }

   $params: map.merge($params, components.$c-productlist-lineitem);
}

.#{component("ProductList:LineItem", "class")} {
   display: flex;
   align-items: center;
   justify-content: space-between;
   flex: 1 0 100%;
   gap: map.get($params, "item-gap");
   padding: map.get($params, "item-padding");
   border-bottom: map.get($params, "item-border-bottom");

   &.borderless {
      border-bottom: 0;
   }

   > a {
      flex: 1;
      display: flex;
      align-items: center;
      gap: map.get($params, "item-gap");
      text-decoration: none;

      @include global.haf {
         .title {
            text-decoration: underline;
         }
      }
   }

   .#{component("Picture", "class")} {
      @if (map.get($params, "image-overlay") == true) {
         @include global.image-overlay(map.get($params, "image-overlay-padding"));
      }

      width: map.get($params, "image-width");
      border: map.get($params, "image-border");
      border-radius: map.get($params, "image-border-radius");
      overflow: hidden;

      img {
         margin: auto;
      }
   }

   .#{component("Product:DiscountLabel", "class")} {
      position: absolute;
      left: 10px;
      top: 10px;
   }

   .content {
      flex: 1;
   }

   .title {
      font-size: map.get($params, "title-font-size");
      font-weight: map.get($params, "title-font-weight");
      color: map.get($params, "title-color");
      margin: map.get($params, "title-margin");
      height: calc(map.get($params, "title-line-clamp") * map.get($params, "item-line-height") * map.get($params, "title-font-size"));
      overflow: hidden;
      -webkit-line-clamp: map.get($params, "title-line-clamp");
      display: -webkit-box;
      -webkit-box-orient: vertical;
   }

   .#{component("Product:Price", "class")} {
      display: flex;
      align-items: baseline;
      flex-wrap: wrap;
      margin: map.get($params, "price-margin");

      > div {
         margin: 0 5px 0 0;

         strong {
            font-size: map.get($params, "price-font-size");
            font-weight: map.get($params, "price-font-weight");
         }
      }

      p strong {
         color: map.get($params, "original-price-color");
         font-size: map.get($params, "original-price-font-size");
      }
   }

   .#{component("Product:DeliveryTime:Availability", "class")} {
      margin: 0;

      p {
         font-size: map.get($params, "delivery-font-size");
         font-weight: map.get($params, "delivery-font-weight");
      }
   }

   .#{component("Product:AddToCartButton", "class")} {
      padding-right: map.get($params, "addtocart-padding-right");
      padding-left: map.get($params, "addtocart-padding-left");

      > .c-icon {
         position: relative;
         left: unset;
      }

      .text {
         display: none;
      }
   }

   &.price-beside-title {
      .#{component("Product:BuyForm", "class")} {
         max-width: map.get($params, "button-no-text-size");

         .btn {
            width: map.get($params, "button-no-text-size");
            height: map.get($params, "button-no-text-size");
            padding: 0;

            .c-icon {
               font-size: map.get($params, "button-no-text-icon-size");
            }
         }
      }

      .#{component("Product:AddToCartButton", "class")} {
         > .c-icon {
            margin-right: 0;
         }
      }

      .#{component("Picture", "class")} {
         width: map.get($params, "image-width-price-beside-title");
      }
   }

   @container (inline-size > 450px) {
      @include global.container-query-selector("&") {
         gap: map.get($params, "item-gap-lg");
         padding: map.get($params, "item-padding-lg");

         @include global.container-query-selector("> a") {
            gap: map.get($params, "item-gap-lg");
         }
      }

      @include global.container-query-selector("&.price-beside-title") {
         @include global.container-query-selector(".content") {
            display: flex;
            align-items: center;
            justify-content: space-between;

            @include global.container-query-selector("> div") {
               min-width: map.get($params, "price-beside-title-width");
            }
         }

         @include global.container-query-selector(".#{component("Product:Price", "class")}") {
            text-align: right;
            flex-direction: column;
            align-items: flex-end;

            @include global.container-query-selector("> div") {
               margin: 0;
            }
         }

         @include global.container-query-selector(".#{component("Product:DeliveryTime:Availability", "class")}") {
            text-align: right;
         }

         @include global.container-query-selector(".title") {
            margin-bottom: 0;
            height: auto;
            max-height: calc(map.get($params, "title-line-clamp") * map.get($params, "item-line-height") * map.get($params, "title-font-size"));
         }
      }

      @include global.container-query-selector("&:not(.price-beside-title)") {
         @include global.container-query-selector(".#{component("Picture", "class")}") {
            width: map.get($params, "image-width-lg");
         }
      }
   }

   @container (inline-size > 660px) {
      @include global.container-query-selector(".#{component("Product:BuyForm", "class")}") {
         max-width: map.get($params, "button-max-width");
         flex: 1;
      }

      @include global.container-query-selector("&.price-beside-title") {
         @include global.container-query-selector(".#{component("Product:BuyForm", "class")}") {
            max-width: map.get($params, "button-no-text-size");
         }
      }

      @include global.container-query-selector("&:not(.price-beside-title)") {
         @include global.container-query-selector(".#{component("Product:AddToCartButton", "class")}") {
            padding-right: map.get($params, "addtocart-padding-right-lg");
            padding-left: map.get($params, "addtocart-padding-left-lg");

            @include global.container-query-selector("> .c-icon") {
               display: none;
            }

            @include global.container-query-selector(".text") {
               display: inline;
            }
         }
      }
   }

   @container (inline-size > #{global.$lg}) {
      @include global.container-query-selector("&") {
         flex: 1 0 21%;
         max-width: calc(25% - #{global.$gap-width});
         border-bottom: 0;
      }

      @include global.container-query-selector("&.has-buyform") {
         flex: 1 0 45%;
         max-width: calc(50% - #{global.$gap-width});
      }

      @include global.container-query-selector("&.price-beside-title") {
         flex: 1 0 100%;
         max-width: calc(100% - #{global.$gap-width});
      }
   }
}
