"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.COLOR_LAYERS_ORDER_CHANGED = void 0;
var _Layer = require("../Layer/Layer");
function updateLayersOrdering(geometryLayer, imageryLayers) {
  const sequence = _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
  const cO = function (object) {
    var _object$material;
    if ((_object$material = object.material) !== null && _object$material !== void 0 && _object$material.setSequence) {
      object.material.setSequence(sequence);
    }
  };
  for (const node of geometryLayer.level0Nodes) {
    node.traverse(cO);
  }
}
const COLOR_LAYERS_ORDER_CHANGED = 'layers-order-changed';

/**
 * Utilitary to organize {@link ColorLayer} in a {@link View}.
 *
 * @module ColorLayersOrdering
 */
exports.COLOR_LAYERS_ORDER_CHANGED = COLOR_LAYERS_ORDER_CHANGED;
var _default = {
  /**
   * Moves up in the layer list. This function has no effect if the layer is
   * moved to its current index.
   *
   * @param {View} view - The view in which the layer is moved up.
   * @param {string} layerId - The ID of the layer to move.
   *
   * @example
   * itowns.ColorLayersOrdering.moveLayerUp(viewer, 'idLayerToUp');
   */
  moveLayerUp(view, layerId) {
    const imageryLayers = view.getLayers(l => l.isColorLayer);
    const layer = view.getLayerById(layerId);
    if (layer) {
      const previousSequence = _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
      _Layer.ImageryLayers.moveLayerUp(layer, imageryLayers);
      updateLayersOrdering(view.tileLayer, imageryLayers);
      view.dispatchEvent({
        type: COLOR_LAYERS_ORDER_CHANGED,
        previous: {
          sequence: previousSequence
        },
        new: {
          sequence: _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers)
        }
      });
      view.notifyChange(view.tileLayer);
    } else {
      throw new Error(`${layerId} isn't color layer`);
    }
  },
  /**
   * Moves down in the layer list. This function has no effect if the layer is
   * moved to its current index.
   *
   * @param {View} view - The view in which the layer is moved down.
   * @param {string} layerId - The ID of the layer to move.
   *
   * @example
   * itowns.ColorLayersOrdering.moveLayerDown(viewer, 'idLayerToDown');
   */
  moveLayerDown(view, layerId) {
    const imageryLayers = view.getLayers(l => l.isColorLayer);
    const layer = view.getLayerById(layerId);
    if (layer) {
      const previousSequence = _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
      _Layer.ImageryLayers.moveLayerDown(layer, imageryLayers);
      updateLayersOrdering(view.tileLayer, imageryLayers);
      view.dispatchEvent({
        type: COLOR_LAYERS_ORDER_CHANGED,
        previous: {
          sequence: previousSequence
        },
        new: {
          sequence: _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers)
        }
      });
      view.notifyChange(view.tileLayer);
    } else {
      throw new Error(`${layerId} isn't color layer`);
    }
  },
  /**
   * Moves a specific layer to a specific index in the layer list. This
   * function has no effect if the layer is moved to its current index.
   *
   * @param {View} view - The view in which the layer is moved.
   * @param {string} layerId - The ID of the layer to move.
   * @param {number} index - The index to move the layer to.
   *
   * @example
   * itowns.ColorLayersOrdering.moveLayerToIndex(viewer, 'idLayerToChangeIndex', 2);
   */
  moveLayerToIndex(view, layerId, index) {
    const imageryLayers = view.getLayers(l => l.isColorLayer);
    const layer = view.getLayerById(layerId);
    if (layer) {
      const previousSequence = _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
      _Layer.ImageryLayers.moveLayerToIndex(layer, index, imageryLayers);
      updateLayersOrdering(view.tileLayer, imageryLayers);
      view.dispatchEvent({
        type: COLOR_LAYERS_ORDER_CHANGED,
        previous: {
          sequence: previousSequence
        },
        new: {
          sequence: _Layer.ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers)
        }
      });
      view.notifyChange(view.tileLayer);
    } else {
      throw new Error(`${layerId} isn't color layer`);
    }
  }
};
exports.default = _default;