"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const source_1 = require("./source");
/**
 * ControlsSource is a Source for the Interactive scene/controls.
 */
class ControlsSource extends source_1.Source {
    constructor() {
        super(...arguments);
        this.updater = new source_1.ResourceComparator({
            id: 'sceneID',
            create: scenes => ({
                method: 'onSceneCreate',
                params: { scenes },
            }),
            update: scenes => ({
                method: 'onSceneUpdate',
                params: { scenes },
            }),
            destroy: sceneID => ({
                method: 'onSceneDelete',
                params: { sceneID, reassignSceneID: 'default' },
            }),
            nested: {
                controls: {
                    id: 'controlID',
                    create: (controls, scene) => ({
                        method: 'onControlCreate',
                        params: { controls, sceneID: scene.sceneID },
                    }),
                    update: (controls, scene) => ({
                        method: 'onControlUpdate',
                        params: { controls, sceneID: scene.sceneID },
                    }),
                    destroy: (controlID, scene) => ({
                        method: 'onControlDelete',
                        params: {
                            sceneID: scene.sceneID,
                            controls: [{ controlID }],
                        },
                    }),
                },
            },
        });
    }
    /**
     * @override
     */
    compare(previous, next) {
        if (Array.isArray(previous)) {
            previous = { scenes: previous };
        }
        if (Array.isArray(next)) {
            next = { scenes: next };
        }
        const sceneUpdates = this.updater.compare(previous.scenes, next.scenes);
        const prevWithoutScenes = Object.assign({}, previous, { scenes: null });
        const nextWithoutScenes = Object.assign({}, next, { scenes: null });
        if (!lodash_1.isEqual(nextWithoutScenes, prevWithoutScenes)) {
            sceneUpdates.push({
                method: 'onWorldUpdate',
                params: next,
            });
        }
        return sceneUpdates;
    }
    /**
     * @override
     */
    sourceProp() {
        return 'scenes';
    }
    /**
     * @override
     */
    createPacket(source) {
        if (Array.isArray(source)) {
            source = { scenes: source };
        }
        return [
            {
                method: 'onSceneCreate',
                params: { scenes: source.scenes },
            },
            {
                method: 'onWorldUpdate',
                params: Object.assign({}, source, { scenes: undefined }),
            },
        ];
    }
}
exports.ControlsSource = ControlsSource;
