<!--
 * Copyright (c) 2022 - present TinyVue Authors.
 * Copyright (c) 2022 - present Huawei Cloud Computing Technologies Co., Ltd.
 *
 * Use of this source code is governed by an MIT-style license.
 *
 * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
 * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
 * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
 *
 -->
<template>
  <label
    v-if="!state.isDisplayOnly || state.model === label"
    class="tiny-radio"
    :class="[
      border && state.radioSize ? 'tiny-radio--' + state.radioSize : '',
      { 'is-disabled': state.isDisabled },
      { 'is-focus': state.focus },
      { 'is-bordered': border },
      { 'is-checked': state.model === label },
      { 'is-display-only': state.isDisplayOnly }
    ]"
    role="radio"
    :aria-checked="state.model === label"
    :aria-disabled="state.isDisabled"
    tabindex="-1"
    @keydown.space.stop.prevent="state.model = state.isDisabled ? state.model : label"
    v-bind="a($attrs, ['class', 'style'], true)"
  >
    <span
      class="tiny-radio__input"
      :class="{
        'is-disabled': state.isDisabled,
        'is-checked': state.model === label
      }"
    >
      <span class="tiny-radio__inner" :tabindex="tabindex"></span>
      <input
        ref="radio"
        class="tiny-radio__original"
        :value="label"
        type="radio"
        v-model="state.model"
        @focus="state.focus = true"
        @blur="state.focus = false"
        @change="handleChange"
        :name="name"
        :disabled="state.isDisabled"
        @click.stop
      />
    </span>
    <span class="tiny-radio__label" @keydown.stop>
      <slot>{{ text || label }}</slot>
    </span>
  </label>
</template>

<script lang="ts">
import { renderless, api } from '@opentiny/vue-renderless/radio/vue'
import { props, setup, defineComponent } from '@opentiny/vue-common'
import type { IRadioApi } from '@opentiny/vue-renderless/types/radio.type'
import '@opentiny/vue-theme/radio/index.less'

export default defineComponent({
  emits: ['change', 'update:modelValue'],
  props: [
    ...props,
    'modelValue',
    'events',
    'label',
    'text',
    'disabled',
    'name',
    'border',
    'size',
    'tabindex',
    'displayOnly'
  ],
  setup(props, context) {
    return setup({ props, context, renderless, api }) as unknown as IRadioApi
  }
})
</script>
