/**
 * Created by sam on 10.02.2015.
 */
import * as provenance from 'phovea_core/src/provenance';
var disabled = {};
export function transform(inputs, parameter) {
    var v = inputs[0].value, transform = parameter.transform, bak = parameter.old || v.transform();
    disabled['transform-' + v.id] = true;
    v.transform(transform.scale, transform.rotate);
    delete disabled['transform-' + v.id];
    return {
        inverse: createTransform(inputs[0], bak, transform)
    };
}
export function createTransform(v, t, old) {
    if (old === void 0) { old = null; }
    return {
        meta: provenance.meta('transform ' + v.toString(), provenance.cat.visual),
        id: 'transform',
        f: transform,
        inputs: [v],
        parameter: {
            transform: t,
            old: old
        }
    };
}
export function changeVis(inputs, parameter) {
    var v = inputs[0].value, to = parameter.to, from = parameter.from || v.act.id;
    disabled['switch-' + v.id] = true;
    return v.switchTo(to).then(function () {
        delete disabled['switch-' + v.id];
        return {
            inverse: createChangeVis(inputs[0], from, to)
        };
    });
}
export function createChangeVis(v, to, from) {
    if (from === void 0) { from = null; }
    return {
        meta: provenance.meta('switch vis ' + v.toString(), provenance.cat.visual),
        id: 'changeVis',
        f: changeVis,
        inputs: [v],
        parameter: {
            to: to,
            from: from
        }
    };
}
export function setOption(inputs, parameter) {
    var v = inputs[0].value, name = parameter.name, value = parameter.value, bak = parameter.old || v.option(name);
    disabled['option-' + v.id] = true;
    v.option(name, value);
    delete disabled['option-' + v.id];
    return {
        inverse: createSetOption(inputs[0], name, bak, value)
    };
}
export function createSetOption(v, name, value, old) {
    if (old === void 0) { old = null; }
    return {
        meta: provenance.meta('set option "' + name + +'" of "' + v.toString() + ' to "' + value + '"', provenance.cat.visual),
        id: 'setOption',
        f: setOption,
        inputs: [v],
        parameter: {
            name: name,
            value: value,
            old: old
        }
    };
}
export function attach(graph, v) {
    var m = v.value, id = m.id;
    if (typeof (m.switchTo) === 'function') {
        m.on('changed', function (event, newValue, old) {
            if (disabled['switch-' + id] !== true) {
                console.log('push switch');
                graph.push(createChangeVis(v, newValue.id, old ? old.id : null));
            }
        });
    }
    m.on('transform', function (event, newValue, old) {
        if (disabled['transform-' + id] !== true) {
            console.log('push transform');
            graph.push(createTransform(v, newValue, old));
        }
    });
    m.on('option', function (event, name, newValue, old) {
        if (disabled['option-' + id] !== true) {
            console.log('push option');
            graph.push(createSetOption(v, name, newValue, old));
        }
    });
}
//# sourceMappingURL=multiform.js.map