import { relativePositionToJSON, createRelativePositionFromJSON } from '../../../../yjs@13.6.26/node_modules/yjs/dist/yjs.js';

/**
 * Defines a range on text using relative positions that can be transformed back to
 * absolute positions. (https://docs.yjs.dev/api/relative-positions)
 */
class YRange {
  /**
   * @param {Y.RelativePosition} yanchor
   * @param {Y.RelativePosition} yhead
   */
  constructor (yanchor, yhead) {
    this.yanchor = yanchor;
    this.yhead = yhead;
  }

  /**
   * @returns {any}
   */
  toJSON () {
    return {
      yanchor: relativePositionToJSON(this.yanchor),
      yhead: relativePositionToJSON(this.yhead)
    }
  }

  /**
   * @param {any} json
   * @return {YRange}
   */
  static fromJSON (json) {
    return new YRange(createRelativePositionFromJSON(json.yanchor), createRelativePositionFromJSON(json.yhead))
  }
}

export { YRange };
