import BaseTile from "@common/documents/tile.mjs";
import CanvasDocumentMixin from "./abstract/canvas-document.mjs";
import {RectangleShapeData} from "@client/data/shapes.mjs";

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

  /**
   * The rectangle shape of this Tile document.
   * @type {RectangleShapeData}
   */
  shape = this.shape; // Workaround for subclass property instantiation issue.

  /* -------------------------------------------- */

  /** @inheritDoc */
  prepareBaseData() {
    super.prepareBaseData();
    /** @deprecated since v14 */
    Object.defineProperty(this.occlusion, "mode", {
      get() {
        foundry.utils.logCompatibilityWarning("TileDocument#occlusion.mode is deprecated in favor of TileDocument#occlusion.modes.",
          {since: 14, until: 16, once: true});
        return this.modes.first() ?? CONST.OCCLUSION_MODES.NONE;
      }
    });
  }

  /* -------------------------------------------- */

  /** @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);
    }
    this.shape = new RectangleShapeData({
      x: this.x,
      y: this.y,
      width: this.width,
      height: this.height,
      anchorX: this.texture.anchorX,
      anchorY: this.texture.anchorY,
      rotation: this.rotation
    }, {parent: this});
  }
}
