'use strict'

const assert = require('check-types').assert

const Origin = require('./')

/**
 * When a {@link Change} is generated by restoring a previous version, this
 * records the original version. We also store the timestamp of the restored
 * version for display; technically, this is redundant, because we could
 * recover it using the version ID. However, it would be very expensive to
 * recover all referenced versions, and it is also possible that the change
 * for the restored version will no longer exist, either because it was merged
 * with other changes or was deleted.
 *
 * @see Origin
 */
class RestoreOrigin extends Origin {
  /**
   * @param {number} version that was restored
   * @param {Date} timestamp from the restored version
   */
  constructor(version, timestamp) {
    assert.integer(version, 'RestoreOrigin: bad version')
    assert.date(timestamp, 'RestoreOrigin: bad timestamp')

    super(RestoreOrigin.KIND)
    this.version = version
    this.timestamp = timestamp
  }

  static fromRaw(raw) {
    return new RestoreOrigin(raw.version, new Date(raw.timestamp))
  }

  /** @inheritdoc */
  toRaw() {
    return {
      kind: RestoreOrigin.KIND,
      version: this.version,
      timestamp: this.timestamp.toISOString(),
    }
  }

  /**
   * @return {number}
   */
  getVersion() {
    return this.version
  }

  /**
   * @return {Date}
   */
  getTimestamp() {
    return this.timestamp
  }
}

RestoreOrigin.KIND = 'restore'

module.exports = RestoreOrigin
