// @flow
import * as _ from 'lodash'
import Layer from './layer'
import {TimelaneScheme} from './scheme/timelane'

export default class TimeLane
{
    static deserialize(timelaneJson: TimelaneScheme)
    {
        const timelane = new TimeLane
        const config = _.pick(timelaneJson.config, ['name']) as TimelaneScheme
        const layers = timelaneJson.layers.map(layerJson => Layer.deserialize(layerJson))

        Object.defineProperty(timelane, 'id', {value: timelaneJson.id})
        timelane.layers = new Set<Layer>(layers)
        Object.assign(timelane.config, config)

        return timelane
    }

    id: string|null = null
    layers: Set<Layer> = new Set()

    config: {
        name: string|null,
    } = {
        name: null
    }

    get name(): string { return (this.config.name as string) }
    set name(name: string) { this.config.name = name }

    constructor()
    {
        Object.seal(this)
    }

    toPreBSON(): Object
    {
        return {
            id: this.id,
            config: Object.assign({}, this.config),
            layers: Array.from(this.layers.values()).map(layer => layer.toPreBSON()),
        }
    }

    toJSON(): Object
    {
        return {
            id: this.id,
            config: Object.assign({}, this.config),
            layers: Array.from(this.layers.values()).map(layer => layer.toJSON()),
        }
    }
}

// @flow
import * as _ from 'lodash'
import Clip from './clip'
import {TimelaneScheme} from './scheme/timelane'

export default class TimeLane
{
    static deserialize(timelaneJson: TimelaneScheme)
    {
        const timelane = new TimeLane
        const config = _.pick(timelaneJson.config, ['name']) as TimelaneScheme
        const layers = timelaneJson.layers.map(layerJson => Clip.deserialize(layerJson))

        Object.defineProperty(timelane, 'id', {value: timelaneJson.id})
        timelane.layers = new Set<Clip>(layers)
        Object.assign(timelane.config, config)

        return timelane
    }

    id: string|null = null
    layers: Set<Clip> = new Set()

    config: {
        name: string|null,
    } = {
        name: null
    }

    get name(): string { return (this.config.name as string) }
    set name(name: string) { this.config.name = name }

    constructor()
    {
        Object.seal(this)
    }

    toPreBSON(): Object
    {
        return {
            id: this.id,
            config: Object.assign({}, this.config),
            layers: Array.from(this.layers.values()).map(layer => layer.toPreBSON()),
        }
    }

    toJSON(): Object
    {
        return {
            id: this.id,
            config: Object.assign({}, this.config),
            layers: Array.from(this.layers.values()).map(layer => layer.toJSON()),
        }
    }
}

Replace reference to clip on Timelane
Replace reference to clip on Timelane
