// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

// DO NOT REMOVE
// Imports Quasar Cypress AE predefined commands
// import { registerCommands } from '@quasar/quasar-app-extension-testing-e2e-cypress';
Cypress.Commands.add('login', (user) => {
    //cy.visit('/#/login');
    cy.request({
        method: 'POST',
        url: '/api/accounts/login',
        body: {
            user: user,
            password: 'nightmare',
        },
    }).then((response) => {
        window.localStorage.setItem('token', response.body.token);
    });
});

Cypress.Commands.add('waitForElement', (element) => {
    cy.get(element, { timeout: 2000 }).should('be.visible');
});

Cypress.Commands.add('getValue', (selector) => {
    cy.get(selector).then(($el) => {
        if ($el.find('.q-checkbox__inner').length > 0) {
            return cy.get(selector + '.q-checkbox__inner');
        }
        // Si es un QSelect
        else if ($el.find('.q-select__dropdown-icon').length) {
            return cy.get(
                selector +
                    '> .q-field > .q-field__inner > .q-field__control > .q-field__control-container > .q-field__native > input'
            );
        } else {
            // Puedes aƱadir un log o lanzar un error si el elemento no es reconocido
            cy.log('Elemento no soportado');
        }
    });
});

// Fill Inputs
Cypress.Commands.add('selectOption', (selector, option) => {
    cy.get(selector).find('.q-select__dropdown-icon').click();
    cy.get('.q-menu .q-item').contains(option).click();
});

Cypress.Commands.add('checkOption', (selector) => {
    cy.wrap(selector).find('.q-checkbox__inner').click();
});

// Global buttons
Cypress.Commands.add('saveCard', () => {
    cy.get('[title="Save"]').click();
});
Cypress.Commands.add('resetCard', () => {
    cy.get('[title="Reset"]').click();
});
Cypress.Commands.add('removeCard', () => {
    cy.get('[title="Remove"]').click();
});
Cypress.Commands.add('addCard', () => {
    cy.waitForElement('tbody');
    cy.get('.q-page-sticky > div > .q-btn').click();
});
Cypress.Commands.add('clickConfirm', () => {
    cy.get('.q-btn--unelevated > .q-btn__content > .block').click();
});

Cypress.Commands.add('fillRow', (rowSelector, data) => {
    // Usar el selector proporcionado para obtener la fila deseada
    cy.waitForElement('tbody');
    cy.get(rowSelector).as('currentRow');

    data.forEach((value, index) => {
        if (value === null) return;
        cy.get('@currentRow')
            .find('td')
            .eq(index)
            .then((td) => {
                if (td.find('.q-select__dropdown-icon').length) {
                    cy.selectOption(td, value);
                }
                if (td.find('.q-checkbox__inner').length && value) {
                    cy.checkOption(td);
                }
            });
    });
});

Cypress.Commands.add('validateRow', (rowSelector, expectedValues) => {
    cy.waitForElement('tbody');
    cy.get(rowSelector).within(() => {
        for (const [index, value] of expectedValues.entries()) {
            cy.log('CHECKING ', index, value);
            if (typeof value == 'boolean') {
                const prefix = value ? '' : 'not.';
                cy.getValue(`:nth-child(${index + 1})`).should(`${prefix}be.checked`);
                continue;
            }
            cy.getValue(`:nth-child(${index + 1})`).should('have.value', value);
        }
    });
});
// registerCommands();