import PlaceableTab from "./placeable-tab.mjs";
import TileFilter from "../filters/tile-filter.mjs";

/**
 * The Tile-specific placeables tab.
 * @extends {PlaceableTab}
 */
export default class TileTab extends PlaceableTab {
  constructor(...args) {
    super(...args);
    Object.assign(this._filterState, {
      occlusionModes: new Set(),
      restrictions: { light: false, weather: false }
    });
  }

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

  /** @override */
  static FILTER_CLASS = TileFilter;

  /* -------------------------------------------- */
  /*  Rendering                                   */
  /* -------------------------------------------- */

  /** @override */
  _getEntryLabel(entry) {
    return entry.name
      || (entry.texture.src ? foundry.audio.AudioHelper.getDefaultSoundName(entry.texture.src) : entry.id);
  }

  /* -------------------------------------------- */
  /*  Filtering                                   */
  /* -------------------------------------------- */

  /** @inheritDoc */
  _clearFilters() {
    super._clearFilters();
    this._filterState.occlusionModes = new Set();
    this._filterState.restrictions = { light: false, weather: false };
  }

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

  /** @inheritDoc */
  _matchesFilter(entry) {
    const { occlusionModes, restrictions } = this._filterState;
    if ( occlusionModes.size ) {
      const hasMode = occlusionModes.some(m => {
        return m === CONST.OCCLUSION_MODES.NONE ? entry.occlusion.modes.size === 0 : entry.occlusion.modes.has(m);
      });
      if ( !hasMode ) return false;
    }
    if ( restrictions.light && !entry.restrictions.light ) return false;
    if ( restrictions.weather && !entry.restrictions.weather ) return false;
    return true;
  }

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

  /** @inheritDoc */
  _hasAdvancedFilters() {
    const { occlusionModes, restrictions } = this._filterState;
    return super._hasAdvancedFilters() || (occlusionModes.size > 0) || restrictions.light || restrictions.weather;
  }
}
