import {BaseSopOperation} from "./_Base";
import {InputCloneMode as InputCloneMode2} from "../../../engine/poly/InputCloneMode";
import {Poly as Poly2} from "../../../engine/Poly";
import {MAG_FILTER_DEFAULT_VALUE, MIN_FILTER_DEFAULT_VALUE} from "../../../core/cop/ConstantFilter";
export class TexturePropertiesSopOperation extends BaseSopOperation {
  static type() {
    return "textureProperties";
  }
  async cook(input_contents, params) {
    const core_group = input_contents[0];
    const objects = [];
    for (let object of core_group.objects()) {
      if (params.applyToChildren) {
        object.traverse((child) => {
          objects.push(child);
        });
      } else {
        objects.push(object);
      }
    }
    const promises = objects.map((object) => this._update_object(object, params));
    await Promise.all(promises);
    return core_group;
  }
  async _update_object(object, params) {
    const material = object.material;
    if (material) {
      await this._update_material(material, params);
    }
  }
  async _update_material(material, params) {
    let texture = material.map;
    if (texture) {
      await this._update_texture(texture, params);
    }
  }
  async _update_texture(texture, params) {
    if (params.tanisotropy) {
      await this._update_anisotropy(texture, params);
    }
    if (params.tminFilter || params.tmagFilter) {
      this._update_filter(texture, params);
    }
  }
  async _update_anisotropy(texture, params) {
    if (params.useRendererMaxAnisotropy) {
      const renderer = await Poly2.renderersController.firstRenderer();
      if (renderer) {
        texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
      }
    } else {
      texture.anisotropy = params.anisotropy;
    }
  }
  _update_filter(texture, params) {
    if (params.tminFilter) {
      texture.minFilter = params.minFilter;
    }
    if (params.tmagFilter) {
      texture.magFilter = params.magFilter;
    }
  }
}
TexturePropertiesSopOperation.DEFAULT_PARAMS = {
  applyToChildren: false,
  tanisotropy: false,
  useRendererMaxAnisotropy: false,
  anisotropy: 1,
  tminFilter: false,
  minFilter: MIN_FILTER_DEFAULT_VALUE,
  tmagFilter: false,
  magFilter: MAG_FILTER_DEFAULT_VALUE
};
TexturePropertiesSopOperation.INPUT_CLONED_STATE = InputCloneMode2.FROM_NODE;
