<script setup>

import { uuidv4 } from '../utils.js';

const props = defineProps({
  label: String,
  id: String,
  disabled: null
});

const model = defineModel({ type: Boolean });

const internalId = uuidv4();

const emit = defineEmits(['change']);

function onChange(event) {
  emit('change', model.value); // this contains the value after the checkbox has already toggled
}

</script>

<template>
  <label :for="id || internalId" class="pankow-switch" :class="{ 'pankow-switch-disabled': disabled }">
    <input :id="id || internalId" class="pankow-switch-input" type="checkbox" v-model="model" :disabled="disabled" @change="onChange"/>
    <span></span>
    <div class="pankow-switch-input-label">
      <slot>{{ label }}</slot>
    </div>
  </label>
</template>

<style>

.pankow-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  margin-bottom: 0;
}

.pankow-switch-disabled,
.pankow-switch-disabled span {
  cursor: not-allowed !important;
}

.pankow-switch span {
  position: relative;
  background-color: var(--pankow-input-background-color);
  border-radius: 17px;
  border-style: solid;
  border-width: 1px;
  border-color: var(--pankow-input-border-color);
  cursor: pointer;
  width: 40px;
  height: 20px;
  transition: all 250ms;
}

.pankow-switch span::before {
  background-color: var(--pankow-text-color);
  border-radius: 50%;
  content: "";
  position: absolute;
  left: 2px;
  bottom: 2px;
  height: calc(100% - 4px);
  aspect-ratio: 1 / 1;
  transition: all 250ms;
}

.pankow-switch-input-label {
  margin-left: 8px;
  cursor: pointer;
}

.pankow-switch-disabled .pankow-switch-input-label {
  cursor: not-allowed;
}

input:checked + span {
  background-color: var(--pankow-color-primary);
}

input:checked + span::before {
  transform: translateX(20px);
  background-color: white;
}

.pankow-switch input {
  display: none;
}

</style>