<template>
  <div class="coop-tag-selector">
    <div
      v-for="tag in tags"
      :key="tag.slug"
      class="coop-row"
    >
      <span class="tag-label">{{ tag.label }}</span>
      <div class="segmented">
        <span
          v-for="opt in options"
          :key="opt.value"
          class="seg-option"
          :class="{ on: getState(tag.slug) === opt.value }"
          @click="toggleState(tag.slug, opt.value)"
        >{{ opt.label }}</span>
      </div>
    </div>
    <div class="suggest-link">
      <span @click="$emit('suggest')">Don't see what you're looking for?</span>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  modelValue: { type: Array, default: () => [] },
  tags: { type: Array, default: () => [] },
});

const emit = defineEmits(["update:modelValue", "suggest"]);

const options = [
  { label: "Can help", value: "help" },
  { label: "Interested", value: "interested" },
  { label: "Need help", value: "seeking" },
];

function getState(slug) {
  const entry = props.modelValue.find((e) => e.tagSlug === slug);
  return entry ? entry.state : null;
}

function toggleState(slug, value) {
  const current = [...props.modelValue];
  const idx = current.findIndex((e) => e.tagSlug === slug);
  const existingState = idx !== -1 ? current[idx].state : null;

  if (existingState === value) {
    // clicking active state deselects it
    if (idx !== -1) current.splice(idx, 1);
  } else if (idx !== -1) {
    current[idx] = { tagSlug: slug, state: value };
  } else {
    current.push({ tagSlug: slug, state: value });
  }

  emit("update:modelValue", current);
}
</script>

<style scoped>
.coop-tag-selector {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.coop-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px dashed var(--border);
}

.coop-row:first-child {
  border-top: 1px dashed var(--border);
}

.tag-label {
  font-size: 11px;
  font-family: "Commit Mono", monospace;
  color: var(--text-dim);
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.segmented {
  display: inline-flex;
  gap: 0;
  flex-shrink: 0;
}

.seg-option {
  padding: 2px 7px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
  color: var(--text-faint);
  font-size: 9px;
  font-family: "Commit Mono", monospace;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: all 0.12s;
  user-select: none;
  white-space: nowrap;
  position: relative;
}

.seg-option + .seg-option {
  margin-left: -1px;
}

.seg-option:hover {
  color: var(--text-dim);
}

.seg-option.on {
  background: var(--surface);
  color: var(--text-bright);
  border-color: var(--candle);
  border-style: solid;
  z-index: 1;
}

.suggest-link {
  font-size: 10px;
  font-family: "Commit Mono", monospace;
  color: var(--text-faint);
  margin-top: 8px;
}

.suggest-link span {
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.suggest-link span:hover {
  color: var(--text-dim);
}
</style>
