<script setup>
import { computed } from "vue";
import { useNamespace, useLocale, useSKU } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { skuSelectorProps, skuSelectorEmits } from "./sku-selector";
defineOptions({ name: "YhSkuSelector" });
const props = defineProps(skuSelectorProps);
const emit = defineEmits(skuSelectorEmits);
const ns = useNamespace("sku-selector");
const { t } = useLocale();
const { themeStyle } = useComponentTheme(
  "sku-selector",
  computed(() => props.themeOverrides)
);
const { selectedValueIds, isValueSelectable, selectedSku, toggleValue } = useSKU(
  props.specs,
  props.skus,
  props.modelValue,
  props.checkStock
);
const handleValueClick = (index, spec, value) => {
  emit("select", spec, value);
  if (isValueActive(value.id) && !props.allowUnselect) return;
  if (props.disabled || !isValueSelectable(index, value.id)) return;
  toggleValue(index, value.id);
  emit("update:modelValue", [...selectedValueIds.value]);
  emit("change", selectedSku.value, [...selectedValueIds.value]);
};
const isValueActive = (valueId) => {
  return selectedValueIds.value.includes(valueId);
};
const summaryPrefixText = computed(() => props.summaryPrefix || t("skuselector.selected"));
const summaryPlaceholder = computed(() => props.placeholder ?? t("skuselector.placeholder"));
const selectedSummary = computed(() => {
  if (!props.showSelectedSummary) return "";
  const names = props.specs.map((spec) => {
    const selectedId = selectedValueIds.value.find((id) => spec.values.some((v) => v.id === id));
    return spec.values.find((v) => v.id === selectedId)?.name ?? "";
  }).filter(Boolean);
  return names.length ? `${summaryPrefixText.value}: ${names.join(" / ")}` : "";
});
const imgSizePx = computed(() => `${props.imageSize}px`);
</script>

<template>
  <div
    :class="[ns.b(), ns.m(size), ns.is('disabled', disabled)]"
    :style="[themeStyle, {
  '--yh-sku-img-size': imgSizePx
}]"
    role="group"
  >
    <div v-if="showSelectedSummary" :class="ns.e('summary')">
      <slot name="summary" :summary="selectedSummary" :sku="selectedSku">
        <span v-if="selectedSummary" :class="ns.e('summary-text')">{{ selectedSummary }}</span>
        <span v-else :class="ns.e('summary-placeholder')">{{ summaryPlaceholder }}</span>
      </slot>
    </div>

    <div v-for="(spec, index) in specs" :key="spec.id" :class="ns.e('item')">
      <div :class="ns.e('label')">
        <slot name="label" :spec="spec">
          <span :class="ns.e('label-name')">{{ spec.name }}</span>
        </slot>
      </div>

      <div :class="[ns.e('values'), spec.mode ? ns.em('values', spec.mode) : '']">
        <div
          v-for="value in spec.values"
          :key="value.id"
          :class="[ns.e('value'), spec.mode ? ns.em('value', spec.mode) : '', ns.is('active', isValueActive(value.id)), ns.is('disabled', !isValueSelectable(index, value.id))]"
          role="radio"
          :aria-checked="isValueActive(value.id)"
          :aria-disabled="!isValueSelectable(index, value.id)"
          @click="handleValueClick(index, spec, value)"
        >
          <slot
            name="value"
            :value="value"
            :spec="spec"
            :active="isValueActive(value.id)"
            :disabled="!isValueSelectable(index, value.id)"
          >
            <span
              v-if="spec.mode === 'color'"
              :class="ns.e('color-swatch')"
              :style="{
  backgroundColor: value.color
}"
            />

            <span v-else-if="spec.mode === 'image'" :class="ns.e('img-wrap')">
              <img
                v-if="value.image"
                :src="value.image"
                :alt="value.name"
                :class="ns.e('item-img')"
              />
            </span>

            <span :class="ns.e('value-name')">{{ value.name }}</span>
            <span v-if="value.tag" :class="ns.e('value-tag')">{{ value.tag }}</span>
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-sku-selector {
  --yh-sku-label-color: var(--yh-text-color-secondary);
  --yh-sku-label-size: var(--yh-font-size-sm);
  --yh-sku-label-weight: var(--yh-font-weight-medium);
  --yh-sku-value-bg: var(--yh-fill-color-light);
  --yh-sku-value-color: var(--yh-text-color-primary);
  --yh-sku-value-border: var(--yh-border-color-light);
  --yh-sku-value-hover-bg: var(--yh-fill-color-dark);
  --yh-sku-value-active-bg: var(--yh-color-primary-light-9);
  --yh-sku-value-active-color: var(--yh-color-primary);
  --yh-sku-value-active-border: var(--yh-color-primary);
  --yh-sku-value-disabled-bg: var(--yh-fill-color-extra-light);
  --yh-sku-value-disabled-color: var(--yh-text-color-disabled);
  --yh-sku-value-radius: var(--yh-radius-base);
  --yh-sku-img-size: 80px;
  --yh-sku-color-swatch-size: 24px;
  --yh-sku-gap: var(--yh-spacing-md);
  display: flex;
  flex-direction: column;
  gap: var(--yh-sku-gap);
}
.yh-sku-selector.is-disabled {
  pointer-events: none;
  opacity: 0.55;
}

.yh-sku-selector__summary {
  font-size: var(--yh-font-size-sm);
  padding-bottom: 2px;
}

.yh-sku-selector__summary-text {
  color: var(--yh-text-color-primary);
  font-weight: var(--yh-font-weight-medium);
}

.yh-sku-selector__summary-placeholder {
  color: var(--yh-text-color-placeholder);
}

.yh-sku-selector__item {
  display: flex;
  flex-direction: column;
  gap: var(--yh-spacing-sm);
}

.yh-sku-selector__label {
  display: flex;
  align-items: center;
  gap: 6px;
}

.yh-sku-selector__label-name {
  font-size: var(--yh-sku-label-size);
  color: var(--yh-sku-label-color);
  font-weight: var(--yh-sku-label-weight);
}

.yh-sku-selector__values {
  display: flex;
  flex-wrap: wrap;
  gap: var(--yh-spacing-sm);
}

.yh-sku-selector__value {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  border-radius: var(--yh-sku-value-radius);
  background-color: var(--yh-sku-value-bg);
  color: var(--yh-sku-value-color);
  border: 1px solid var(--yh-sku-value-border);
  cursor: pointer;
  transition: all var(--yh-transition-base);
  user-select: none;
  font-size: var(--yh-font-size-base);
  position: relative;
  overflow: hidden;
  white-space: nowrap;
}
.yh-sku-selector__value:hover:not(.is-disabled):not(.is-active) {
  background-color: var(--yh-sku-value-hover-bg);
  border-color: var(--yh-border-color);
}
.yh-sku-selector__value.is-active {
  background-color: var(--yh-sku-value-active-bg);
  border-color: var(--yh-sku-value-active-border);
  color: var(--yh-sku-value-active-color);
  font-weight: var(--yh-font-weight-medium);
  transition: all var(--yh-transition-base);
}
.yh-sku-selector__value.is-active::after {
  content: "";
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 14px;
  height: 14px;
  background: var(--yh-color-primary);
  border-radius: 4px 0 0 0;
  z-index: 1;
}
.yh-sku-selector__value.is-active::before {
  content: "";
  position: absolute;
  bottom: 3px;
  right: 2px;
  width: 6px;
  height: 3px;
  border-left: 1.5px solid white;
  border-bottom: 1.5px solid white;
  transform: rotate(-45deg);
  z-index: 2;
}

.yh-sku-selector__value.is-disabled {
  cursor: not-allowed;
  background-color: var(--yh-sku-value-disabled-bg);
  color: var(--yh-sku-value-disabled-color);
  border: 1px dashed var(--yh-border-color-lighter);
  opacity: 0.6;
  transition: none;
}
.yh-sku-selector__value.is-disabled::after, .yh-sku-selector__value.is-disabled::before {
  display: none;
}

.yh-sku-selector__value--image {
  padding: 0;
  flex-direction: column;
  gap: 0;
  overflow: hidden;
  min-width: var(--yh-sku-img-size);
  background: transparent;
  border-radius: var(--yh-radius-base);
}

.yh-sku-selector__value--text .yh-sku-selector__value-name {
  padding: 0;
  max-width: unset;
}

.yh-sku-selector__value--color {
  padding: 3px;
  min-width: auto;
  gap: 0;
}

.yh-sku-selector__img-wrap {
  display: block;
  width: var(--yh-sku-img-size);
  height: var(--yh-sku-img-size);
  overflow: hidden;
  border-radius: calc(var(--yh-sku-value-radius) - 1px);
  background-color: var(--yh-fill-color-light);
  flex-shrink: 0;
}

.yh-sku-selector__item-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform var(--yh-transition-base);
}
.yh-sku-selector__item-img:hover {
  transform: scale(1.05);
}

.yh-sku-selector__value-name {
  display: block;
  text-align: center;
  padding: 4px 6px;
  font-size: var(--yh-font-size-xs);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: var(--yh-sku-img-size);
}

.yh-sku-selector__color-swatch {
  display: block;
  width: var(--yh-sku-color-swatch-size);
  height: var(--yh-sku-color-swatch-size);
  border-radius: calc(var(--yh-sku-value-radius) - 2px);
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.yh-sku-selector__value-tag {
  display: inline-flex;
  align-items: center;
  padding: 0 4px;
  height: 14px;
  font-size: 10px;
  border-radius: 2px;
  line-height: 1;
  background: var(--yh-color-danger-light-9);
  color: var(--yh-color-danger);
  font-weight: 500;
  flex-shrink: 0;
}

.yh-sku-selector--small .yh-sku-selector__value {
  padding: 4px 10px;
  font-size: var(--yh-font-size-xs);
}

.yh-sku-selector--large .yh-sku-selector__value {
  padding: 8px 18px;
  font-size: var(--yh-font-size-md);
}
</style>
