var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_attach_image_from_icon_exports = {};
__export(use_attach_image_from_icon_exports, {
  useAttachImageFromIcon: () => useAttachImageFromIcon
});
module.exports = __toCommonJS(use_attach_image_from_icon_exports);
var import_lodash_es = require("lodash");
var import_react = require("react");
var import_client = require("react-dom/client");
var import_maplibre = require("react-map-gl/maplibre");
var import_use_safe_svg_parser = require("./use-safe-svg-parser.js");
var import_constants = require("../constants.js");
var import_attach_image_to_map = require("../functions/attach-image-to-map.js");
var import_create_bitmap_config_options = require("../functions/create-bitmap-config-options.js");
var import_get_data_uri = require("../functions/get-data-uri.js");
var import_get_scaled_symbol_size = require("../functions/get-scaled-symbol-size.js");
const useAttachImageFromIcon = (icon, suffix, outputSize) => {
  const { elementAsString, ref } = (0, import_use_safe_svg_parser.useSafeSvgParser)();
  const defaultScaledIconSize = (0, import_get_scaled_symbol_size.getScaledSymbolSize)();
  const { current: map } = (0, import_maplibre.useMap)();
  const img = new Image(defaultScaledIconSize, defaultScaledIconSize);
  const fakeDomContainer = document.createElement("div");
  (0, import_react.useLayoutEffect)(() => {
    if ((0, import_lodash_es.isString)(icon)) {
      return;
    }
    const reactNodeRefContainer = (0, import_react.createElement)("div", { ref }, icon);
    const root = (0, import_client.createRoot)(fakeDomContainer);
    root.render(reactNodeRefContainer);
  }, [icon]);
  fakeDomContainer.remove();
  const stringUrl = (0, import_get_data_uri.getDataUri)(elementAsString);
  img.addEventListener("load", () => {
    createImageBitmap(
      img,
      0,
      0,
      import_constants.DEFAULT_INPUT_ICON_SIZE,
      import_constants.DEFAULT_INPUT_ICON_SIZE,
      (0, import_create_bitmap_config_options.createBitmapConfigOptions)(outputSize)
    ).then((bitmap) => {
      const iconName = `custom-icon-${suffix}`;
      (0, import_attach_image_to_map.attachImageToMap)(map, bitmap, iconName);
    });
  });
  img.src = stringUrl;
  return !(0, import_lodash_es.isString)(icon);
};
