import BaseAmbientSound from "@common/documents/ambient-sound.mjs";
import CanvasDocumentMixin from "./abstract/canvas-document.mjs";
import {CircleShapeData} from "@client/data/shapes.mjs";

/**
 * The client-side AmbientSound document which extends the common BaseAmbientSound document model.
 * @extends BaseAmbientSound
 * @mixes ClientDocumentMixin
 * @category Documents
 *
 * @see {@link foundry.documents.Scene}: The Scene document type which contains AmbientSound documents
 * @see {@link foundry.applications.sheets.AmbientSoundConfig}: The AmbientSound configuration
 *   application
 */
export default class AmbientSoundDocument extends CanvasDocumentMixin(BaseAmbientSound) {

  /**
   * The circle shape of this AmbientSound document.
   * @type {CircleShapeData}
   */
  shape = this.shape; // Workaround for subclass property instantiation issue.

  /* -------------------------------------------- */
  /*  Model Methods                               */
  /* -------------------------------------------- */

  /** @inheritDoc */
  prepareDerivedData() {
    super.prepareDerivedData();
    if ( this.parent ) {
      const d = this.parent.dimensions;
      this.x = Math.clamp(this.x, 0, d.width);
      this.y = Math.clamp(this.y, 0, d.height);
    }
    const grid = this.parent?.grid ?? foundry.documents.BaseScene.defaultGrid;
    const distancePixels = grid.size / grid.distance;
    const {x, y} = this;
    const radius = this.radius * distancePixels;
    this.shape = new CircleShapeData({x, y, radius}, {parent: this});
  }
}
