/*
 * Copyright 2023 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

/* eslint-env mocha */
import assert from 'assert';

import { deedFormContent, deedFormResultWithOutPlainText, deedFormResultWithPlainText } from './resources/deed.js';
import { getAllForms } from '../src/FormsImporter.js';
import {
  customerServiceOrderContent,
  customerServiceOrderResult,
  customerServiceOrderResultWithFieldSet, customerServiceOrderResultWithHiddenFields,
} from './resources/customer-service-order.js';
import { shippingServiceContent, shippingServiceForm } from './resources/shipping-service.js';
import { creditRequestContent, creditRequestResult } from './resources/credit-request.js';
import { claimFormContent, claimFormED } from './resources/claim.js';
import { FormType } from '../src/support/type.js';

const op = { includeHidden: false, includePlainText: false, includeStats: true };

describe('Form Importer Tests', () => {
  it('Form importer from Deed content', async () => {
    const forms = await getAllForms(deedFormContent, op, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 3);
    const deedForm = forms[2];
    assert.deepStrictEqual(deedForm, deedFormResultWithOutPlainText);
  });

  it('Form importer from Deed content including plain text', async () => {
    const forms = await getAllForms(deedFormContent, { ...op, includePlainText: true }, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 3);
    const deedForm = forms[2];
    assert.deepStrictEqual(deedForm, deedFormResultWithPlainText);
  });

  it('Form importer from customer service order exclude hidden fields', async () => {
    const options = { ...op, includeHidden: false, includePlainText: false };
    const forms = await getAllForms(customerServiceOrderContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const customerService = forms[0];
    assert.deepStrictEqual(customerService, customerServiceOrderResult);
  });

  it('Form importer from customer service order including hidden fields', async () => {
    const options = { ...op, includeHidden: true, includePlainText: false };
    const forms = await getAllForms(customerServiceOrderContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const customerService = forms[0];
    assert.deepStrictEqual(customerService, customerServiceOrderResultWithHiddenFields);
  });

  it('Form importer from customer Service invalid selector grouping', async () => {
    const options = {
      ...op, includeHidden: true, includePlainText: true, groupBySelector: '. aem-GridColumn',
    };
    const forms = await getAllForms(customerServiceOrderContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const customerService = forms[0];
    assert.deepStrictEqual(customerService, customerServiceOrderResultWithHiddenFields);
  });

  it('Form importer from customer service order including fieldset', async () => {
    const options = {
      ...op, includeHidden: true, includePlainText: true, groupBySelector: '.aem-GridColumn',
    };
    const forms = await getAllForms(customerServiceOrderContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const customerService = forms[0];
    assert.deepStrictEqual(customerService, customerServiceOrderResultWithFieldSet);
  });

  it('Form importer from Shipping Service including fieldset', async () => {
    const options = {
      ...op, includeHidden: true, includePlainText: true, groupBySelector: '.aem-GridColumn',
    };
    const forms = await getAllForms(shippingServiceContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const form = forms[0];
    assert.deepStrictEqual(form, shippingServiceForm);
  });

  it('Form importer from Credit request including fieldset with Headers', async () => {
    const options = {
      ...op, includeHidden: true, includePlainText: true, groupBySelector: '.aem-GridColumn',
    };
    const forms = await getAllForms(creditRequestContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const form = forms[0];
    assert.deepStrictEqual(form, creditRequestResult);
  });

  it('Form importer claim form', async () => {
    const options = { ...op, includeHidden: false, includePlainText: false };
    const forms = await getAllForms(claimFormContent, options, FormType.EDFORM);
    assert(forms !== undefined);
    assert.strictEqual(forms.length, 1);
    const form = forms[0];
    assert.deepStrictEqual(form, claimFormED);
  });
});
