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

$params: (
   "width": 100%,
   "color": global.$white,
   "background": global.$brand-ctr,
   "border-color": global.$brand-ctr,
   "border-radius": global.$btn-border-radius,

   "icon-size": 20px,
   "icon-left-absolute": "true",
   "icon-right-absolute": "false",
   "icon-left": 20px,
   "icon-right": 20px,
   "icon-margin-right": global.$gap-width,

   "loader-size": 30px,
);

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

   $params: map.merge($params, components.$c-product-addtocartbutton);
}

.#{component("Product:AddToCartButton", "class")} {
   @include global.button-variant(map.get($params, "color"), map.get($params, "background"), map.get($params, "border-color"));

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

   @if (map.get($params, "icon-left-absolute") == "true") {
      &:has(> .c-icon) {
         padding-left: calc((#{map.get($params, "icon-left")} * 1.5) + #{map.get($params, "icon-size")});
      }
   } @else if (map.get($params, "icon-right-absolute") == "true") {
      &:has(> .c-icon) {
         padding-right: calc((#{map.get($params, "icon-right")} * 1.5) + #{map.get($params, "icon-size")});
      }
   }

   > .c-icon {
      font-size: map.get($params, "icon-size");

      @if (map.get($params, "icon-left-absolute") == "true") {
         position: absolute;
         left: map.get($params, "icon-left");
      } @else if (map.get($params, "icon-right-absolute") == "true") {
         position: absolute;
         right: map.get($params, "icon-right");
      } @else {
         margin-right: map.get($params, "icon-margin-right");
      }
   }

   .product-added-success {
      position: absolute;
      inset: -(global.$btn-border-width);
      z-index: 1;
      background-color: map.get($params, "background");
      color: map.get($params, "color");
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0;

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

   &.btn-product-added .product-added-success {
      opacity: 1;
      z-index: 1;
   }

   .add-to-cart-loader {
      position: absolute;
      inset: -(global.$btn-border-width);
      z-index: 1;
      background-color: map.get($params, "background");

      &::before {
         animation: add-to-cart-loader 1.25s infinite linear;
         border: 3px solid map.get($params, "color");
         border-radius: 50%;
         border-right-color: transparent;
         content: "";
         display: block;
         position: absolute;
         left: calc(50% - (#{map.get($params, "loader-size")} / 2));
         top: calc(50% - (#{map.get($params, "loader-size")} / 2));
         width: map.get($params, "loader-size");
         height: map.get($params, "loader-size");
      }
   }

   .non-variation {
      @include global.btn-disabled;

      display: none;
      align-items: center;
      justify-content: center;
      position: absolute;
      inset: -(global.$btn-border-width);
      z-index: 1;
   }

   &.has-variation-error {
      @include global.btn-disabled;

      pointer-events: auto;
      cursor: not-allowed;

      .non-variation {
         display: flex;
      }
   }

   &:not(:last-child) {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
   }
}

@keyframes add-to-cart-loader {
   0% {
      transform: rotate(0);
   }

   100% {
      transform: rotate(360deg);
   }
}
