/* Copyright 2020 Mozilla Foundation
 *
 * Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import {
  $dump,
  $getChildren,
  $getChildrenByClass,
  $getChildrenByName,
  $text,
} from "../../src/core/xfa/xfa_object.js";
import { Binder } from "../../src/core/xfa/bind.js";
import { searchNode } from "../../src/core/xfa/som.js";
import { XFAParser } from "../../src/core/xfa/parser.js";
describe("XFAParser", function () {
  describe("Parse XFA", function () {
    it("should parse a xfa document and create an object to represent it", function () {
      const xml = `
  
    
      
        
          7
        
      
      foobar
    
    
      http://a.b.c
      
        
          forbidden
        
      
      enabled
      
                 http://d.e.f
      
      http://g.h.i
      foobar
    
  
  
    
      1.23
      1
      314
      2.71
    
    
      
        
        
        
        
        
      
    
  
      `;
      const attributes = {
        id: "",
        name: "",
        use: "",
        usehref: "",
      };
      const mediumAttributes = {
        id: "",
        long: 0,
        orientation: "portrait",
        short: 0,
        stock: "",
        trayIn: "auto",
        trayOut: "auto",
        use: "",
        usehref: "",
      };
      const colorAttributes = {
        cSpace: "SRGB",
        id: "",
        use: "",
        usehref: "",
      };
      const root = new XFAParser().parse(xml);
      const expected = {
        uuid: "1234",
        timeStamp: "",
        template: {
          baseProfile: "full",
          extras: {
            ...attributes,
            float: [
              { ...attributes, $content: 1.23 },
              { ...attributes, $content: 2.71 },
            ],
            boolean: { ...attributes, $content: 1 },
            integer: { ...attributes, $content: 314 },
          },
          subform: {
            access: "open",
            allowMacro: 0,
            anchorType: "topLeft",
            colSpan: 1,
            columnWidths: [0],
            h: "",
            hAlign: "left",
            id: "",
            layout: "position",
            locale: "",
            maxH: 0,
            maxW: 0,
            mergeMode: "consumeData",
            minH: 0,
            minW: 0,
            name: "",
            presence: "visible",
            relevant: [],
            restoreState: "manual",
            scope: "name",
            use: "",
            usehref: "",
            w: "",
            x: 0,
            y: 0,
            proto: {
              area: {
                ...attributes,
                colSpan: 1,
                x: 0,
                y: -226.08,
                relevant: [
                  { excluded: true, viewname: "foo" },
                  { excluded: false, viewname: "bar" },
                ],
              },
              color: [
                {
                  ...colorAttributes,
                  value: { r: 111, g: 222, b: 123 },
                },
                {
                  ...colorAttributes,
                  value: { r: 111, g: 0, b: 123 },
                },
              ],
              medium: [
                {
                  ...mediumAttributes,
                  imagingBBox: {
                    x: 1,
                    y: 144,
                    width: 96.3779527559055,
                    height: 5.67,
                  },
                },
                {
                  ...mediumAttributes,
                  imagingBBox: {
                    x: -1,
                    y: -1,
                    width: -1,
                    height: -1,
                  },
                },
              ],
            },
          },
        },
        config: {
          acrobat: {
            acrobat7: {
              dynamicRender: {
                $content: "forbidden",
              },
            },
            autoSave: {
              $content: "enabled",
            },
            validate: {
              $content: "preSubmit",
            },
            submitUrl: [
              {
                $content: "http://a.b.c",
              },
              {
                $content: "http://d.e.f",
              },
              {
                $content: "http://g.h.i",
              },
            ],
          },
          present: {
            pdf: {
              name: "hello",
              adobeExtensionLevel: {
                $content: 7,
              },
            },
          },
        },
      };
      expect(root[$dump]()).toEqual(expected);
    });
    it("should parse a xfa document and check namespaces", function () {
      const xml = `
  
    
      
        
          7
        
      
    
    
      http://a.b.c
      http://c.b.a
    
  
  
    
      1.23
    
  
      `;
      const root = new XFAParser().parse(xml);
      const expected = {
        uuid: "",
        timeStamp: "",
        config: {
          acrobat: {
            submitUrl: { $content: "http://c.b.a" },
          },
        },
      };
      expect(root[$dump]()).toEqual(expected);
    });
    it("should parse a xfa document and parse CDATA when needed", function () {
      const xml = `
  
    
      
        
          
            
              hello