"use strict";

import {expect} from "chai"
import {buildTree} from "../../../source/data/buildtree.mjs";
import {NodeList} from "../../../source/types/nodelist.mjs";


describe('buildTree', function () {


    describe('legacy navigation example (issue #230)', function () {

        it('should run example', function () {

            const objects = JSON.parse(`{
    "dataset": [
        {
            "id": 100001,
            "parent_id": 200001,
            "title": "eBay",
            "url": "/",
            "weight": 1,
            "css_styles": "",
            "css_classes": ""
        },
        {
            "id": 200001,
            "parent_id": 200001,
            "title": "alvineconsole",
            "url": "",
            "weight": 1,
            "css_styles": "",
            "css_classes": ""
        }
    ],
    "sys": {
        "code": 200,
        "result": {},
        "api_version": "1"
    }
}`);

            try {
                buildTree(objects, 'dataset.*', 'id', 'parent_id');    
            } catch (error) {
                expect(error).to.be.instanceOf(Error);
            }
            

            

        })
    })    
    

    describe('example', function () {

        it('should run example', function () {

            const objects = {
                dataset: {
                    "1": {
                        id: "1",
                        name: "vehicle",
                        parent: null

                    },
                    "2": {
                        id: "2",
                        name: "car",
                        parent: "1"
                    },
                    "3": {
                        id: "3",
                        name: "truck",
                        parent: "1"
                    },
                    "4": {
                        id: "4",
                        name: "motorcycle",
                        parent: "1"
                    },
                    "5": {
                        id: "5",
                        name: "SUV",
                        parent: "2"
                    },
                    "6": {
                        id: "6",
                        name: "sports car",
                        parent: "2"
                    }
                }
            }

            const nodes = buildTree(objects, 'dataset.*', 'id', 'parent');

            expect(nodes).to.be.instanceOf(NodeList);
            expect(nodes.length).to.equal(1);
            expect(nodes.toString()).to.be.equal('{"id":"1","name":"vehicle","parent":null}\n ├{"id":"2","name":"car","parent":"1"}\n | ├{"id":"5","name":"SUV","parent":"2"}\n | └{"id":"6","name":"sports car","parent":"2"}\n ├{"id":"3","name":"truck","parent":"1"}\n └{"id":"4","name":"motorcycle","parent":"1"}' );


        })
    })

    describe('buildTree()', function () {

        let obj1 = {
            dataset: {
                "1": {
                    id: "1",
                    name: "Test 1",
                    address: "Here 1",
                    parent: null

                },
                "2": {
                    id: "2",
                    name: "Test 2",
                    address: "Here 2",
                    parent: "1"
                },
                "3": {
                    id: "3",
                    name: "Test 3",
                    address: "Here 3",
                    parent: "2"
                },
                "4": {
                    id: "4",
                    name: "Test 4",
                    address: "Here 4",
                    parent: "2"
                },
                "5": {
                    id: "5",
                    name: "Test 5",
                    address: "Here 5",
                    parent: "1"
                },
                "6": {
                    id: "6",
                    name: "Test 6",
                    address: "Here 6",
                    parent: "4"
                }
            }
        };

        it('should return Node Instance.', function () {
            expect(buildTree({}, '')).to.be.instanceOf(NodeList);
        });

        [
            [obj1, 'dataset.*', '{"id":"1","name":"Test 1","address":"Here 1","parent":null}\n ├{"id":"2","name":"Test 2","address":"Here 2","parent":"1"}\n | ├{"id":"3","name":"Test 3","address":"Here 3","parent":"2"}\n | └{"id":"4","name":"Test 4","address":"Here 4","parent":"2"}\n | | └{"id":"6","name":"Test 6","address":"Here 6","parent":"4"}\n └{"id":"5","name":"Test 5","address":"Here 5","parent":"1"}'],
            [obj1, 'x', ''],
            [obj1, 'dataset.*', '{"id":"1","name":"Test 1","address":"Here 1","parent":null}\n ├{"id":"2","name":"Test 2","address":"Here 2","parent":"1"}\n | ├{"id":"3","name":"Test 3","address":"Here 3","parent":"2"}\n | └{"id":"4","name":"Test 4","address":"Here 4","parent":"2"}\n | | └{"id":"6","name":"Test 6","address":"Here 6","parent":"4"}\n └{"id":"5","name":"Test 5","address":"Here 5","parent":"1"}'],
        ].forEach(function (data) {

            let a = data.shift()
            let b = data.shift()
            let c = data.shift()

            it('buildTree(' + JSON.stringify(a) + ',' + JSON.stringify(b) + ') should return ' + JSON.stringify(c) + ' ', function () {
                let nodelist = buildTree(a, b, 'id', 'parent');
                expect(nodelist.toString()).to.be.eql(c);
            });
        });


    });

    describe('buildTree() test 2', function () {

        /**
         * 2
         *    1
         *    3
         * 4
         *    6
         * 5
         *
         */
        let obj1 = {
            dataset: {
                "1": {
                    id: "1",
                    name: "Test 1",
                    address: "Here 1",
                    parent: "2"
                },
                "2": {
                    id: "2",
                    name: "Test 2",
                    address: "Here 2",
                    parent: "0"
                },
                "3": {
                    id: "3",
                    name: "Test 3",
                    address: "Here 3",
                    parent: "2"
                },
                "4": {
                    id: "4",
                    name: "Test 4",
                    address: "Here 4",
                    parent: "0"
                },
                "5": {
                    id: "5",
                    name: "Test 5",
                    address: "Here 5",
                    parent: "0"
                },
                "6": {
                    id: "6",
                    name: "Test 6",
                    address: "Here 6",
                    parent: "4"
                },
                "7": {
                    id: "7",
                    name: "Test 7",
                    address: "Here 7",
                    parent: "1"
                }
            }
        };

        it('should return Node Instance.', function () {
            expect(buildTree({}, '')).to.be.instanceOf(NodeList);
        });

        [
            [obj1, 'dataset.*', '{"id":"2","name":"Test 2","address":"Here 2","parent":"0"}\n ├{"id":"1","name":"Test 1","address":"Here 1","parent":"2"}\n | └{"id":"7","name":"Test 7","address":"Here 7","parent":"1"}\n └{"id":"3","name":"Test 3","address":"Here 3","parent":"2"}\n{"id":"4","name":"Test 4","address":"Here 4","parent":"0"}\n └{"id":"6","name":"Test 6","address":"Here 6","parent":"4"}\n{"id":"5","name":"Test 5","address":"Here 5","parent":"0"}'],
            [obj1, 'x', ''],
        ].forEach(function (data) {

            let a = data.shift()
            let b = data.shift()
            let c = data.shift()

            it('buildTree(' + JSON.stringify(a) + ',' + JSON.stringify(b) + ') should return ' + JSON.stringify(c) + ' ', function () {
                let nodelist = buildTree(a, b, 'id', 'parent', {
                    rootReferences: ['0']
                });

                expect(nodelist.toString()).to.be.equal(c);

            });
        });


    });
    
    
    


});
