import dayjs from "dayjs";
export const MockData = [{
        url: 'http://datatable.test',
        method: 'POST',
        status: 200,
        response: (request) => {
            var _a, _b;
            const body = typeof request.body === 'string' ? JSON.parse(request.body) : request.body;
            const data = exampleData((body === null || body === void 0 ? void 0 : body.limit) ? body.limit * 3 : 5);
            let response = {
                data: {
                    count: data.length,
                    items: data,
                },
            };
            if (body) {
                if (body.search) {
                    response.data.items = response.data.items.filter(function (i) {
                        return i.description.toLowerCase().includes(body.search.toLowerCase());
                    });
                }
                if (body.filters && Object.keys(body.filters).length) {
                    Object.keys(body.filters).forEach(key => {
                        const type = body.filters[key].type;
                        const condition = body.filters[key].condition;
                        const value = type == 'date' ? dayjs(body.filters[key].value).unix() : body.filters[key].value;
                        response.data.items = response.data.items.filter(item => {
                            const itemValue = type == 'date' ? dayjs(item[key]).unix() : item[key];
                            switch (condition) {
                                case '>':
                                    return itemValue > value;
                                case '<':
                                    return itemValue < value;
                                case '>=':
                                    return itemValue >= value;
                                case '<=':
                                    return itemValue <= value;
                                case '!=':
                                    return String(itemValue).toLowerCase() !== String(value).toLowerCase();
                                case '[]':
                                    return value.split(',').includes(String(itemValue));
                                case '*':
                                    return String(itemValue).trim().toLowerCase().includes(String(value).trim().toLowerCase());
                                case '~': {
                                    const [min, max] = value.split(',').map(Number);
                                    return itemValue >= min && itemValue <= max;
                                }
                                case '><': {
                                    const [start, end] = value.split(',').map(v => v.trim());
                                    return itemValue <= start && itemValue >= end;
                                }
                                case '=':
                                default:
                                    return String(itemValue).toLowerCase() === String(value).toLowerCase();
                            }
                        });
                    });
                }
                if ((_a = body.sort) === null || _a === void 0 ? void 0 : _a.field) {
                    const orderField = body.sort.field;
                    const direction = ((_b = body.sort.dir) !== null && _b !== void 0 ? _b : 'asc').toLowerCase();
                    response.data.items.sort((a, b) => {
                        if (a[orderField] < b[orderField])
                            return direction === 'asc' ? -1 : 1;
                        if (a[orderField] > b[orderField])
                            return direction === 'asc' ? 1 : -1;
                        return 0;
                    });
                }
                response.data.count = response.data.items.length;
                if (body.limit) {
                    response.data.items = response.data.items.splice(0, body.limit);
                    response.data.cursor = body.cursor ? parseInt(body.cursor) + body.limit : body.limit;
                    response.data.more = response.data.cursor < response.data.count;
                }
                if (body.group) {
                    const groupField = body.group.field;
                    const direction = (body.group.dir || 'asc').toLowerCase();
                    response.data.items = response.data.items.sort((a, b) => {
                        if (a[groupField] < b[groupField])
                            return direction === 'asc' ? -1 : 1;
                        if (a[groupField] > b[groupField])
                            return direction === 'asc' ? 1 : -1;
                        return 0;
                    }).reduce((groups, row) => {
                        const groupValue = row[groupField];
                        let group = groups.find(g => g.value === groupValue);
                        if (!group) {
                            group = { value: groupValue, label: String(groupValue).charAt(0).toUpperCase() + String(groupValue).slice(1), items: [] };
                            groups.push(group);
                        }
                        group.items.push(row);
                        return groups;
                    }, []);
                }
            }
            return response;
        },
    }];
// Dependencias necesarias para el mock
const icons = ["bug", "clock", "danger"];
const descriptions = [
    "Bug fix by",
    "Pending task by",
    "Failed reported by"
];
const statuses = ["success", "pending", "failed"];
const avatars = [
    "https://www.gravatar.com/avatar/2a30fc5b79e49fc85e6245897c5bda7d",
    "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50",
    "https://www.gravatar.com/avatar/c1f67dd86116068f2067918fe3c8f56c"
];
const names = [
    "Ivan Suazo", "John Doe", "Anonymous", "Maria Lopez", "Carlos Perez",
    "Elena Ruiz", "Luis Gomez", "Ana Torres", "Pedro Sanchez", "Lucia Diaz",
    "David Ortega", "Sofia Ramos", "Miguel Castro", "Laura Vega", "Jorge Silva",
    "Paula Romero", "Andres Molina", "Carmen Herrera", "Victor Cruz", "Sara Morales"
];
function randomFrom(arr) {
    return arr[Math.floor(Math.random() * arr.length)];
}
function randomDate(start, end) {
    const date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
    return date.toISOString().replace("T", " ").substring(0, 19);
}
export function exampleData(size) {
    return Array.from({ length: size }, (_, i) => {
        const idx = i % 3;
        const name = randomFrom(names);
        const now = dayjs();
        return {
            id: i + 1,
            name: name,
            icon: icons[idx],
            description: `${descriptions[idx]} ${name}`,
            time: +(Math.random() * 8 + 1).toFixed(1),
            amount: +(Math.random() * 2000 + 5).toFixed(2),
            date: randomDate(new Date(now.year(), now.month(), now.startOf('month').date()), new Date(now.year(), now.month(), now.endOf('month').date())),
            status: statuses[idx],
            avatar: avatars[idx]
        };
    });
}
