<template>
    <div :class="checkboxClass" :style="iconColors">
        <checkbox
            :disabled="disabled"
            v-model="checkboxValue">
            <div class="checkbox__label" v-if="label">
                {{ label }}
            </div>
            <template slot="input-box">
                <div class="checkbox">
                    <CheckedIcon
                        :iconColor="themeVariables.white"
                        v-if="isChecked"
                        noHover
                    />
                </div>
            </template>
        </checkbox>
    </div>
</template>

<script lang="ts">
    import Vue from "vue";
    import {Checkbox} from 'vue-checkbox-radio';
    import UncheckedIcon from '../../icons/checkbox/UncheckedIcon.vue';
    import CheckedIcon from '../../icons/checkbox/CheckedIcon.vue';
    // @ts-ignore
    import themeVariables from '../../../assets/scss/colors.scss';

    import {
        CHECKBOX__CHECKED_CLASS,
        CHECKBOX__DISABLED_CLASS, CHECKBOX__SMALL_CLASS,
        CHECKBOX__WITH_ERROR_CLASS,
        CHECKBOX__WRAPPER_CLASS,
    } from "./constants";

    export default Vue.extend({
        name: "CheckboxComponent",
        props: {
            value: {
                type: Boolean,
                default: false,
            },
            disabled: {
                type: Boolean,
                default: false,
            },
            label: {
                type: String,
                default: '',
            },
            hasError: {
                type: Boolean,
                default: false,
            },
            color: {
                type: String,
                default(): string {
                    return themeVariables.primary;
                },
            },
            small: {
                type: Boolean,
                default: false,
            },
        },
        components: {
            Checkbox,
            UncheckedIcon,
            CheckedIcon,
        },
        data() {
            return {
                themeVariables: themeVariables,
            };
        },
        computed: {
            checkboxValue: {
                get(): boolean {
                    return this.value;
                },
                set(value: boolean): void {
                    this.$emit('input', value);
                },
            },
            checkboxClass(): object {
                return {
                    [CHECKBOX__WRAPPER_CLASS]: true,
                    [CHECKBOX__DISABLED_CLASS]: this.disabled,
                    [CHECKBOX__WITH_ERROR_CLASS]: this.hasError,
                    [CHECKBOX__CHECKED_CLASS]: this.isChecked,
                    [CHECKBOX__SMALL_CLASS]: this.small,
                };
            },
            iconColors(): object {
                return {
                    '--color': this.color,
                };
            },
            isChecked(): boolean {
                return this.checkboxValue;
            },
        },
    });
</script>

<style scoped lang="scss">
    @import "checkboxComponent";
</style>