import { createApp, reactive } from 'https://unpkg.com/petite-vue?module';
const profesores = reactive({
    data: [],
    search: null,
    fetch: async function () {
        const response = await fetch('action/action_profesor.php');
        this.data = await response.json();
    },
    get clave() {
        const match = this.search.match(/^\((.+)\)/);
        return match ? match[1] : '';
    },
    get current() {
        return this.data.find((profesor) => profesor.profesor_clave === profesores.clave);
    },
});
const facultades = reactive({
    data: [],
    fetch: async function () {
        const facultades = await fetch('action/action_facultad.php').then(response => response.json());
        const carreras = await fetch(`action/carrera.php`).then(response => response.json());
        this.data = await Promise.all(facultades.map(async (facultad) => ({
            ...facultad,
            carreras: await Promise.all(carreras.filter((carrera) => carrera.facultad_id === facultad.facultad_id).map(async (carrera) => {
                const grupos = await fetch(`action/action_grupo.php?carrera_id=${carrera.carrera_id}`).then(response => response.json());
                return {
                    ...carrera,
                    grupos,
                };
            })),
        })));
        this.data = this.data.filter((facultad) => facultad.carreras.length > 0);
    }
});
const horarios = reactive({
    data: [],
    fetch: async function (grupo = null, carrera_id = null) {
        if (grupo && carrera_id) {
            const response = await fetch(`action/action_horario.php?grupo=${grupo}&carrera_id=${carrera_id}`);
            this.data = await response.json();
        }
        else if (profesores.current) {
            const response = await fetch(`action/action_horario.php?profesor_id=${profesores.current.profesor_id}`);
            this.data = await response.json();
        }
    },
    get structure() {
        if (this.data.length === 0)
            return null;
        const structure = {
            sábado: this.data.some((horario) => horario.horario_dia === 6),
            hora_mínima: Math.min(...this.data.map((horario) => parseInt(horario.horario_hora.split(':')[0]))),
            hora_máxima: Math.max(...this.data.map((horario) => {
                const [hour, minute] = horario.horario_fin.split(':').map(Number);
                return hour + Math.ceil(minute / 60);
            })),
            horas_totales: 0
        };
        structure.horas_totales = structure.hora_máxima - structure.hora_mínima;
        return structure;
    },
    get blocks() {
        if (this.data.length === 0)
            return null;
        return [...Array(this.structure.horas_totales).keys()].flatMap(hora => {
            const baseHour = hora + this.structure.hora_mínima;
            return [0, 15, 30, 45].map(block => ({ hour: baseHour, block }));
        });
    },
    getHorarioData(hour, block, día) {
        const foundHorario = this.data.find((horario) => parseInt(horario.horario_hora.split(':')[0]) === hour &&
            parseInt(horario.horario_hora.split(':')[1]) === block &&
            horario.horario_dia === día);
        return foundHorario;
    },
    isOccupied(hora, bloque, day) {
        if (this.getHorarioData(hora, bloque, day)) {
            return false;
        }
        const currentTimeInMinutes = hora * 60 + bloque;
        for (const item of this.data) {
            if (item.horario_dia !== day) {
                continue; // Skip items that are not on the specified day
            }
            // Split the hour and minute from horario_hora
            const [startHour, startMinute] = item.horario_hora.split(":").map(Number);
            const startTimeInMinutes = startHour * 60 + startMinute;
            // Calculate end time using duracion
            const [durationHours, durationMinutes] = item.duracion.split(":").map(Number);
            const endTimeInMinutes = startTimeInMinutes + (durationHours * 60) + durationMinutes;
            if (currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes) {
                return true; // The block is occupied
            }
        }
        return false; // The block is not occupied by any class
    }
});
const app = createApp({
    profesores,
    horarios,
    facultades,
    mounted: async function () {
        await profesores.fetch();
        await facultades.fetch();
    }
}).mount('#app');
