<template>
    <time :datetime="value.isoFormat()">
        {{ valueStr }}
    </time>
</template>

<script>
import { DateTime, Date, Time } from '@silane/datetime';


const weekdayJa = ['月', '火', '水', '木', '金', '土', '日'];
export default {
    name: 'DateTime',
    props: {
        value: {
            required: true,
            validator(val) {
                return val instanceof Date || val instanceof Time;
            },
        },
        format: {
            type: String,
            default: null,
        }
    },
    computed: {
        valueStr() {
            const value = this.value;
            const format = this.format;
            if(format != null) {
                return value.strftime(format);
            }
            if(value instanceof Date) {
                const thisYear = Date.today().year === value.year;
                let format = thisYear ? '' : '%Y/';
                format += `%m/%d(${weekdayJa[value.weekday()]})`;
                if(value instanceof DateTime) {
                    format += ' %H:%M'
                    if(value.microsecond)
                        format += ':%S.%f';
                    else if(value.second)
                        format += ' :%S';
                    format += '%Z';
                }
                return value.strftime(format);
            }
            if(value instanceof Time) {
                let format = '%H:%M';
                if(value.microsecond)
                    format += ':%S.%f';
                else if(value.second)
                    format += ':%S';
                format += '%Z';
                return value.strftime(format);
            }
        }
    }
}
</script>
