"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var app_1 = require("../app");
var widgetSearcher_1 = require("./widgetSearcher");
var browserHashService_1 = require("../service/browserHashService");
var ComposerEntryPage = (function (_super) {
    __extends(ComposerEntryPage, _super);
    function ComposerEntryPage(path) {
        var _this = _super.call(this) || this;
        _this.widgetSearcher = new widgetSearcher_1.WidgetSearcher();
        if (path) {
            _this.currentPath = path;
        }
        _this.widgetSearcher.events.listenBy(_this, "widget", function (Widget) {
            _this.preview(Widget);
            browserHashService_1.browserHashService.set("path", Widget);
        });
        app_1.App.testResourceMaintainer.events.listenBy(_this, "reloaded", function () {
            _this.setupCommonWidgets();
            if (_this.currentPath)
                _this.preview(_this.currentPath);
        });
        return _this;
    }
    ComposerEntryPage.prototype.setupCommonWidgets = function () {
        Leaf.Widget.namespace.clear();
        if (TestR["Widget"]) {
            var CommonWidget = TestR["Widget"];
            this.recursiveInclude(CommonWidget);
        }
    };
    ComposerEntryPage.prototype.recursiveInclude = function (ws) {
        if (!ws)
            return;
        for (var name_1 in ws) {
            var Cons = ws[name_1];
            if (Cons.prototype instanceof Leaf.Widget) {
                Leaf.Widget.namespace.include(Cons);
                console.log("incluc", Cons);
            }
            else {
                this.recursiveInclude(Cons);
            }
        }
    };
    ComposerEntryPage.prototype.preview = function (path) {
        this.currentPath = path;
        var builder = new FakeWidgetBuilder(path);
        this.widgetPreview = builder.widget;
    };
    return ComposerEntryPage;
}(R.ComposerEntryPage));
exports.ComposerEntryPage = ComposerEntryPage;
var FakeWidgetBuilder = (function () {
    function FakeWidgetBuilder(path, testData, stack) {
        if (testData === void 0) { testData = null; }
        if (stack === void 0) { stack = []; }
        this.path = path;
        this.testData = testData;
        this.stack = stack;
        this.maxRecursive = 3;
        this.BindedWidgets = [];
        this.BindedLists = [];
        var Cons = this.Cons = this.resolve(path);
        var datas = Cons["TestDatas"] || [];
        var test = testData || datas[0] && datas[0].json;
        this.stack.push(path);
        this.BindedWidgets = Cons["BindedWidgets"] || [];
        this.BindedLists = Cons["BindedLists"] || [];
        this.widget = this.createWidget(Cons, test);
    }
    FakeWidgetBuilder.prototype.getWidget = function () {
        return this.widget;
    };
    FakeWidgetBuilder.prototype.getRecursiveDepth = function () {
        var _this = this;
        return this.stack.filter(function (item) { return item === _this.path; }).length;
    };
    FakeWidgetBuilder.prototype.createWidget = function (Cons, data) {
        if (data === void 0) { data = {}; }
        console.error(this.getRecursiveDepth());
        var widget = new Cons();
        for (var _i = 0, _a = this.BindedWidgets; _i < _a.length; _i++) {
            var widgetInfo = _a[_i];
            var test = data[widgetInfo.name] || null;
            if (!widgetInfo.path)
                continue;
            if (this.getRecursiveDepth() > this.maxRecursive)
                break;
            var builder = new FakeWidgetBuilder(widgetInfo.path, test, this.stack.slice());
            widget[widgetInfo.name] = builder.widget;
        }
        for (var _b = 0, _c = this.BindedLists; _b < _c.length; _b++) {
            var listInfo = _c[_b];
            var tests = data[listInfo.name] || [];
            if (!(tests instanceof Array)) {
                tests = [tests];
            }
            if (tests.length === 0) {
                // tree default list item
                tests = [null, null, null];
            }
            var ref = this.resolveListReference(listInfo);
            if (!ref)
                continue;
            if (this.getRecursiveDepth() > this.maxRecursive)
                break;
            for (var _d = 0, tests_1 = tests; _d < tests_1.length; _d++) {
                var test = tests_1[_d];
                var builder = new FakeWidgetBuilder(ref, test, this.stack.slice());
                widget[listInfo.name]["push"](builder.widget);
            }
        }
        for (var name_2 in data) {
            if (widget._VMFields.indexOf(name_2) >= 0) {
                widget.VM[name_2] = data[name_2];
            }
        }
        return widget;
    };
    FakeWidgetBuilder.prototype.hasList = function (name, lists) {
        if (lists === void 0) { lists = this.BindedLists; }
        for (var _i = 0, lists_1 = lists; _i < lists_1.length; _i++) {
            var list = lists_1[_i];
            if (list.name === name)
                return true;
        }
        return false;
    };
    FakeWidgetBuilder.prototype.hasWidget = function (name, widgets) {
        if (widgets === void 0) { widgets = this.BindedWidgets; }
        for (var _i = 0, widgets_1 = widgets; _i < widgets_1.length; _i++) {
            var widget = widgets_1[_i];
            if (name === widget.name)
                return true;
        }
        return false;
    };
    FakeWidgetBuilder.prototype.getWidgetByName = function (name, widgets) {
        if (widgets === void 0) { widgets = this.BindedWidgets; }
        for (var _i = 0, widgets_2 = widgets; _i < widgets_2.length; _i++) {
            var widget = widgets_2[_i];
            if (widget.name === name)
                return widget;
        }
        return null;
    };
    FakeWidgetBuilder.prototype.getListByName = function (name, lists) {
        if (lists === void 0) { lists = this.BindedLists; }
        for (var _i = 0, lists_2 = lists; _i < lists_2.length; _i++) {
            var list = lists_2[_i];
            if (list.name === name)
                return list;
        }
        return null;
    };
    FakeWidgetBuilder.prototype.resolve = function (path) {
        var routes = path.split("/").filter(function (item) { return !!item; }).map(function (item) { return Leaf.Util.capitalize(item); });
        var cur = TestR;
        var name;
        while (name = routes.shift()) {
            cur = cur[name];
        }
        return cur;
    };
    FakeWidgetBuilder.prototype.resolveListReference = function (list) {
        if (list.type == "reference") {
            return list.reference;
        }
        else if (list.type == "inline") {
            return list.reference;
        }
        else {
            return null;
        }
    };
    return FakeWidgetBuilder;
}());
