const {
  addMatchImageSnapshotCommand,
} = require('cypress-image-snapshot/command');

addMatchImageSnapshotCommand();

import chaiColors from 'chai-colors';
chai.use(chaiColors);

Cypress.Commands.add('waitForWindow', () => {
  cy.window().then(() => {
    window._ready = window._ready || false;
    window._onReady = () => {
      window._ready = true;
    };
    // eslint-disable-next-line no-unused-expressions
    !window._listening &&
      document.addEventListener('DOMContentLoaded', window._onReady, false);
    window._listening = true;
    return document.readyState === 'complete' ? true : window._ready;
  });
});

Cypress.Commands.add('openInstance', path => {
  cy.clearCookies();
  cy.visit(path);
  cy.url().should('include', path.substring(path.indexOf('.')));
});

Cypress.Commands.add('iframe', (element, actions) => {
  cy.get(element).then($iframe => {
    actions($iframe);
  });
});

Cypress.Commands.add('eClick', component => {
  cy.get(component).click();
});

Cypress.Commands.add('eContains', (component, text) => {
  cy.get(component, {
    timeout: 100000,
  })
    .contains(text)
    .click({ force: true });
});

Cypress.Commands.add('eType', (component, text) => {
  cy.get(component)
    .clear()
    .type(text, { force: true });
});

Cypress.Commands.add('eTypeForce', (component, text) => {
  cy.get(component).type(text, { force: true });
});

Cypress.Commands.add('iget', (doc, selector) => {
  return cy.wrap(doc.contents().find(selector));
});

Cypress.Commands.add('questions', (entity, elements) => {
  // eslint-disable-next-line no-sparse-arrays
  elements.forEach(function iterator(item, index) {
    cy.xpath(`//${entity}[contains(.,"${item}")]`)
      .eq(index)
      .click();
  });
});

Cypress.Commands.add('typing', (entity, elements) => {
  // eslint-disable-next-line no-sparse-arrays
  elements.forEach(function iterator(item, index) {
    [...item].forEach(function iterator(c) {
      cy.xpath(`//${entity}[contains(.,"${c}")]`)
        .eq(index)
        .click();
    });
  });
});

Cypress.Commands.add('contain', (element, value, page = 0) => {
  if (page == 0) {
    var expected_text = cy
      .get(element)
      .invoke('text')
      .then(text => {
        expect(text.trim()).to.contain(value);
      });
  } else {
    var expected_text = cy
      .iget(page, element)
      .invoke('text')
      .then(text => {
        expect(text.trim()).to.contain(value);
      });
  }
});

Cypress.Commands.add('css', (page = 0, element, field, value) => {
  if (page == 0) {
    cy.get(element)
      .should('have.css', field)
      .and('match', new RegExp(value));
  } else {
    cy.iget(page, element)
      .should('have.css', field)
      .and('match', new RegExp(value));
  }
});

Cypress.Commands.add('cssColor', (page = 0, element, field, value) => {
  if (page == 0) {
    cy.get(element)
      .should('have.css', field)
      .and('be.colored', value);
  } else {
    cy.iget(page, element)
      .should('have.css', field)
      .and('be.colored', value);
  }
});

Cypress.Commands.add('snapshotComponent', (iframe = 0, element) => {
  if (iframe == 0) {
    cy.get(page, element).matchImageSnapshot();
  } else {
    cy.iframe(iframe, function (page) {
      cy.iget(page, element).matchImageSnapshot();
    });
  }
});
