<template>
  <span class="pankow-radio">
    <input :name="name" :id="id || internalId" class="pankow-radio-input" type="radio" v-model="internalValue" :value="value" :disabled="disabled" :required="$attrs['required']"/>
    <slot>
      <label :for="id || internalId" class="pankow-radio-input-label">{{ label }}</label>
    </slot>
  </span>
</template>

<script>

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

export default {
  name: 'Radiobutton',
  props: {
    modelValue: String,
    value: String,
    label: String,
    id: String,
    name: String,
    disabled: null
  },
  emits: ['update:modelValue'],
  computed: {
    internalValue: {
      get() {
        return this.modelValue
      },
      set(internalValue) {
        this.$emit('update:modelValue', internalValue)
      }
    }
  },
  data () {
    return {
      internalId: uuidv4()
    }
  }
};

</script>

<style>

.pankow-radio {
  display: flex;
  white-space: nowrap;
  align-items: center;
  margin: 4px 0;
}

.pankow-radio-input-label {
  display: inline-block;
  cursor: pointer;
  margin: 0;
  margin-left: 6px;
}

.pankow-radio-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: 20px;
}

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

.pankow-radio-input:after {
  content: "";
  display: block;
  position: absolute;
  opacity: var(--state-opacity, 0);
  width: 9px;
  height: 9px;
  background: white;
  border-radius: 20px;
  left: 4px;
  top: 4px;
  transform: rotate(43deg);
}

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

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

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

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

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

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

</style>