<script setup>

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

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

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

const internalId = uuidv4();

</script>

<template>
  <span class="pankow-checkbox">
    <input :id="id || internalId" class="pankow-checkbox-input" type="checkbox" v-model="model" :disabled="disabled" :required="$attrs['required']" @input="$emit('update:modelValue', $event.target.checked)"/>
    <slot>
      <label :for="id || internalId" class="pankow-checkbox-input-label">
        {{ label }}
        <sup v-if="helpUrl"><a :href="helpUrl" target="_blank" tabindex="-1"><i class="fa fa-question-circle"></i></a></sup>
      </label>
    </slot>
  </span>
</template>

<style>

.pankow-checkbox {
  display: flex;
  align-items: end; /* this gives better alignment for the text wrt checkbox */
  white-space: nowrap;
  margin: 4px 0;
}

.pankow-checkbox-input-label {
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
  margin: 0;
  margin-left: 6px;
}

.pankow-checkbox-input {
  appearance: none;
  height: 19px;
  width: 19px;
  outline: none;
  display: inline-block;
  position: relative;
  margin: 0;
  cursor: pointer;
  border: 1px solid #ced4da;
  background: var(--state-background, white);
  transition: background 0.25s, border-color 0.25s, box-shadow 0.25s;
  border-radius: 3px;
}

@media (prefers-color-scheme: dark) {
  .pankow-checkbox-input {
    color: var(--pankow-color-light-dark);
    background: var(--pankow-color-dark);
    border: 1px solid var(--pankow-color-dark);
  }
}

.pankow-checkbox-input:after {
  content: "";
  display: block;
  position: absolute;
  opacity: var(--state-opacity, 0);
  width: 5px;
  height: 8px;
  border: 2px solid white;
  border-top: 0;
  border-left: 0;
  left: 6px;
  top: 3px;
  transform: rotate(43deg);
}

.pankow-checkbox-input:checked {
  --state-background: var(--pankow-color-primary);
  --state-opacity: 1;
  border: 1px solid transparent;
}

.pankow-checkbox-input:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.pankow-checkbox-input:disabled + label {
  cursor: not-allowed;
  opacity: 0.5;
}

.pankow-checkbox-input:hover {
  border: 1px solid var(--pankow-color-dark);
}

@media (prefers-color-scheme: dark) {
  .pankow-checkbox-input:hover {
    border: 1px solid var(--pankow-color-light-dark);
  }
}

.pankow-checkbox-input:focus {
  border: 1px solid var(--pankow-color-primary);
}

</style>