(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./ExpressionParser"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const ExpressionParser_1 = require("./ExpressionParser");
    const isEvent = /^event/i;
    function oneTime(name, b, control, e) {
        control.runAfterInit(() => {
            control.setLocalValue(e, name, b.sourcePath(control, e));
        });
    }
    function event(name, b, control, e) {
        control.runAfterInit(() => {
            if (isEvent.test(name)) {
                name = name.substr(5);
                name = (name[0].toLowerCase() + name.substr(1));
            }
            control.bindEvent(e, name, (e1) => {
                return b.sourcePath(control, e1);
            });
        });
    }
    function oneWay(name, b, control, e, creator) {
        if (b.pathList) {
            control.bind(e, name, b.pathList, false, () => {
                // tslint:disable-next-line: ban-types
                return b.sourcePath.call(creator, control, e);
            });
            return;
        }
        if (b.combined) {
            const a = {
                // it is `this`
                t: creator,
                // it is first parameter
                x: control
            };
            control.bind(e, name, b.combined, false, () => {
                // tslint:disable-next-line: ban-types
                return b.sourcePath.call(creator, control, e);
            }, a);
            return;
        }
        if (b.thisPathList) {
            control.bind(e, name, b.thisPathList, false, () => {
                // tslint:disable-next-line: ban-types
                return b.sourcePath.call(creator, control, e);
            }, creator);
            return;
        }
    }
    function twoWays(name, b, control, e, creator) {
        control.bind(e, name, b.thisPathList || b.pathList, b.eventList || true, null, b.thisPathList ? creator : undefined);
    }
    function presenter(name, b, control, e) {
        const n = b.name || name;
        let c = control.element;
        while (c) {
            if (c.atomControl && c.atomControl[n] !== undefined) {
                break;
            }
            c = c._logicalParent || c.parentElement;
        }
        ((c && c.atomControl) || control)[n] = e;
    }
    class Bind {
        constructor(setupFunction, sourcePath, name, eventList) {
            this.setupFunction = setupFunction;
            this.name = name;
            this.eventList = eventList;
            this.sourcePath = sourcePath;
            if (!this.sourcePath) {
                return;
            }
            if (Array.isArray(this.sourcePath)) {
                this.pathList = this.sourcePath;
                // this.setupFunction = null;
            }
            else {
                const lists = ExpressionParser_1.parsePathLists(this.sourcePath);
                if (lists.combined.length) {
                    this.combined = lists.combined;
                }
                if (lists.pathList.length) {
                    this.pathList = lists.pathList;
                }
                if (lists.thisPath.length) {
                    this.thisPathList = lists.thisPath;
                }
                if (setupFunction === oneWay) {
                    if (!(this.combined || this.pathList || this.thisPathList)) {
                        throw new Error(`Failed to setup binding for ${this.sourcePath}, parsing failed`);
                    }
                }
            }
        }
        static forControl() {
            return Bind;
        }
        static forData() {
            return Bind;
        }
        static forViewModel() {
            return Bind;
        }
        static forLocalViewModel() {
            return Bind;
        }
        static presenter(name) {
            return new Bind(presenter, null, name);
        }
        // tslint:disable-next-line: ban-types
        static event(sourcePath) {
            return new Bind(event, sourcePath);
        }
        static oneTime(sourcePath) {
            return new Bind(oneTime, sourcePath);
        }
        static oneWay(sourcePath) {
            return new Bind(oneWay, sourcePath);
        }
        static twoWays(sourcePath, events) {
            const b = new Bind(twoWays, sourcePath, null, events);
            if (!(b.thisPathList || b.pathList)) {
                throw new Error(`Failed to setup twoWay binding on ${sourcePath}`);
            }
            return b;
        }
        /**
         * Use this for HTML only, this will fire two way binding
         * as soon as the input/textarea box is updated
         * @param sourcePath binding lambda expression
         */
        static twoWaysImmediate(sourcePath) {
            const b = new Bind(twoWays, sourcePath, null, ["change", "input", "paste"]);
            if (!(b.thisPathList || b.pathList)) {
                throw new Error(`Failed to setup twoWay binding on ${sourcePath}`);
            }
            return b;
        }
    }
    exports.default = Bind;
});
//# sourceMappingURL=Bind.js.map