text: // / <reference types="Cypress" />
  
  import SettingsPageObject from '../../../../support/pages/module/sw-settings.page-object';
  
  describe('Flow builder: Test crud operations', () => {
      // eslint-disable-next-line no-undef
      beforeEach(() => {
          cy.loginViaApi().then(() => {
              cy.openInitialPage(`${Cypress.env('admin')}#/sw/flow/index`);
          });
      });
  
      it('@settings: Create and read flow', () => {
          const page = new SettingsPageObject();
  
          cy.intercept({
              url: `${Cypress.env('apiPath')}/flow`,
              method: 'POST'
          }).as('saveData');
  
          cy.get('.sw-flow-list').should('be.visible');
          cy.get('.sw-flow-list__create').click();
  
          // Verify "create" page
          cy.get('.smart-bar__header h2').contains('New flow');
  
          // Fill all fields
          cy.get('#sw-field--flow-name').type('Order placed v1');
          cy.get('#sw-field--flow-priority').type('10');
  
          cy.get('.sw-flow-detail__tab-flow').click();
          cy.get('.sw-flow-trigger__input-field').type('order placed');
          cy.get('.sw-flow-trigger__search-result').should('be.visible');
          cy.get('.sw-flow-trigger__search-result').eq(0).click();
  
          // Save
          cy.get('.sw-flow-detail__save').click();
          cy.wait('@saveData').its('response.statusCode').should('equal', 204);
          cy.get('.sw-flow-leave-page-modal__leave-page').click();
  
          // Verify successful save
          cy.get('.sw-loader__element').should('not.exist');
          cy.get('.smart-bar__header h2').contains('Order placed v1');
  
          // Verify created element
          cy.get(page.elements.smartBarBack).click({force: true});
          cy.get('.sw-flow-leave-page-modal').should('be.visible');
          cy.get('.sw-flow-leave-page-modal__leave-page').click();
          cy.get(`${page.elements.dataGridRow}--0`).should('be.visible')
              .contains('Order placed v1');
      });
  
      it('@settings: Try to create flow with empty required fields', () => {
          cy.intercept({
              url: `${Cypress.env('apiPath')}/flow`,
              method: 'POST'
          }).as('saveEmptyData');
  
          cy.get('.sw-flow-list').should('be.visible');
          cy.get('.sw-flow-list__create').click();
  
          // Verify "create" page
          cy.get('.smart-bar__header h2').contains('New flow');
  
          // Click save without entering any data
          cy.get('.sw-flow-detail__save').click();
  
          // Verify 400 Bad request
          cy.wait('@saveEmptyData').its('response.statusCode').should('equal', 400);
  
          // Automatically jump to tab Flow and check if name field has error message
          cy.get('.sw-flow-detail-general__general-name .sw-field__error')
              .should('be.visible')
              .should('contain', 'This field must not be empty.');
  
          cy.awaitAndCheckNotification('The flow could not be saved.');
  
          cy.get('.sw-flow-detail-general__general-name').type('Order placed');
          cy.get('.sw-flow-detail-general__general-name .sw-field__error')
              .should('not.exist');
  
          cy.get('.sw-flow-detail__save').click();
  
          // Verify 400 Bad request
          cy.wait('@saveEmptyData').its('response.statusCode').should('equal', 400);
  
          // Check if trigger event field ha error message
          cy.get('.sw-flow-trigger__search-field .sw-field__error')
              .should('be.visible')
              .should('contain', 'This field must not be empty.');
  
          cy.awaitAndCheckNotification('The flow could not be saved.');
      });
  
      it('@settings: Update and read flow', () => {
          const page = new SettingsPageObject();
  
          cy.intercept({
              url: `${Cypress.env('apiPath')}/flow/*`,
              method: 'PATCH'
          }).as('updateData');
          cy.intercept({
              url: `${Cypress.env('apiPath')}/search/flow`,
              method: 'POST'
          }).as('getFlow');
  
          cy.get('.sw-flow-list').should('be.visible');
  
          cy.get('input.sw-search-bar__input').typeAndCheckSearchField('Order placed');
          cy.get('.sw-data-grid-skeleton').should('not.exist');
  
          cy.get(`${page.elements.dataGridRow}--0`).should('be.visible')
              .contains('Order placed');
  
          cy.clickContextMenuItem(
              '.sw-flow-list__item-edit',
              page.elements.contextMenuButton,
              `${page.elements.dataGridRow}--0`,
              'Edit',
              true
          );
  
          // Verify correct detail page
          cy.get('.smart-bar__header h2').contains('Order placed');
  
  
          cy.get('#sw-field--flow-name').clearTypeAndCheck('Order placed v2');
          cy.get('.sw-flow-detail__tab-flow').click();
  
          cy.get('.sw-flow-sequence-action__add-button').click();
          cy.get('.sw-flow-sequence-action__selection-action')
              .typeSingleSelect('Generate document', '.sw-flow-sequence-action__selection-action');
  
          cy.get('.sw-flow-generate-document-modal').should('be.visible');
  
          cy.get('.sw-flow-generate-document-modal__type-multi-select').typeMultiSelectAndCheck('Invoice');
  
          cy.get('.sw-flow-generate-document-modal__save-button').click();
          cy.get('.sw-flow-generate-document-modal').should('not.exist');
          cy.get('li.sw-flow-sequence-action__action-item').should('have.length', 2);
  
          cy.get('.sw-flow-detail__save').click();
          cy.wait('@updateData').its('response.statusCode').should('equal', 204);
          cy.wait('@getFlow').its('response.statusCode').should('equal', 200);
          // Verify updated element
          cy.get(page.elements.smartBarBack).click({force: true});
          cy.get('input.sw-search-bar__input').typeAndCheckSearchField('Order placed v2');
          cy.get('.sw-data-grid-skeleton').should('not.exist');
          cy.get(`${page.elements.dataGridRow}--0`).should('be.visible')
              .contains('Order placed v2');
      });
  
      it('@settings: Delete flow', () => {
          const page = new SettingsPageObject();
  
          cy.intercept({
              url: `${Cypress.env('apiPath')}/flow/*`,
              method: 'DELETE'
          }).as('deleteData');
  
          cy.get('.sw-flow-list').should('be.visible');
  
          cy.get('input.sw-search-bar__input').typeAndCheckSearchField('Order placed');
          cy.get('.sw-data-grid-skeleton').should('not.exist');
  
          cy.get(`${page.elements.dataGridRow}--0`).should('be.visible')
              .contains('Order placed');
  
          cy.clickContextMenuItem(
              `${page.elements.contextMenu}-item--danger`,
              page.elements.contextMenuButton,
              `${page.elements.dataGridRow}--0`,
              'Delete',
              true
          );
  
          cy.get('.sw-modal__body')
              .contains('Are you sure you want to delete this item?');
          cy.get(`${page.elements.modal}__footer button${page.elements.dangerButton}`).click();
          cy.get(page.elements.modal).should('not.exist');
  
          cy.wait('@deleteData').its('response.statusCode').should('equal', 204);
  
          cy.get(`${page.elements.dataGridRow}--0 .sw-data-grid__cell--name`)
              .contains('Order placed').should('not.exist');
      });
  });
  