<template>
  <BmsModal
    v-model="isModalOpened"
    :buttons="[]"
    title="Feature Flipper"
    @close="onClose"
  >
    <div class="content">
      <p v-if="Object.keys(features).length === 0">
        Pas de fonctionnalité disponible pour le feature flipping
      </p>
      <div
        v-else
        v-for="name of Object.keys(features)"
        class="feature"
        :key="name"
      >
        <label :for="name">{{ name }}</label>
        <input
          type="checkbox"
          :name="name"
          :checked="isFeatureEnabled(name)"
          @change="toggleFeature(name)"
        />
      </div>
      <slot name="additional"></slot>
    </div>
  </BmsModal>
</template>

<script lang="ts" setup>
import { BmsModal } from '@/index';
import { computed } from 'vue';
import { useFeatureFlipper } from './featureFlipper.composable';

const { features, isFeatureEnabled, toggleFeature } = useFeatureFlipper();

const props = defineProps<{ modelValue: boolean }>();
const emits = defineEmits<{
  (e: 'update:modelValue', payload: boolean): void;
}>();

const isModalOpened = computed({
  get: () => props.modelValue,
  set: (newModelValue) => {
    emits('update:modelValue', newModelValue);
  },
});

const onClose = () => {
  isModalOpened.value = false;
};
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.feature {
  width: 30%;
  display: flex;
  justify-content: space-between;
  margin: 8px;
  font-weight: bold;
}
</style>
