import { Chart as Chart$1, MarkdownText as MarkdownText$1, KpiCard as KpiCard$1, ChoroplethGeoJson as ChoroplethGeoJson$1, ChoroplethVectorTiles as ChoroplethVectorTiles$1, ChoroplethSvg as ChoroplethSvg$1, PoiMap as PoiMap$1 } from '@opendatasoft/visualizations';
import React, { forwardRef, useRef, useEffect } from 'react';
import { useMergeRefs } from 'use-callback-ref';

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

var __assign = function() {
    __assign = Object.assign || function __assign(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};

function __rest(s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
}

// The wrapper build a function component for the given component class
function wrap(ComponentConstructor) {
  // We use forwardRef to forward the actual ref of the container
  return /*#__PURE__*/forwardRef(function (props, forwardedRef) {
    var tag = props.tag,
      data = props.data,
      options = props.options,
      elementProps = __rest(props, ["tag", "data", "options"]);
    // This ref will hold our SDK component instance
    var componentRef = useRef(null);
    // This ref will hold the container element
    var containerRef = useRef(null);
    // By merging container ref and forwarded ref parent component could also access the container ref !
    var ref = useMergeRefs([forwardedRef, containerRef]);
    // Update data (put before creating the component to skip the initial render)
    useEffect(function () {
      var _a;
      (_a = componentRef.current) === null || _a === void 0 ? void 0 : _a.updateData(data);
    }, [data]);
    // Update options (put before creating the component to skip the initial render)
    useEffect(function () {
      var _a;
      (_a = componentRef.current) === null || _a === void 0 ? void 0 : _a.updateOptions(options);
    }, [options]);
    // Create and destroy
    useEffect(function () {
      var container = containerRef.current;
      if (container) {
        var component_1 = new ComponentConstructor(container, data, options);
        componentRef.current = component_1;
        return function () {
          component_1.destroy();
          componentRef.current = null;
        };
      }
      throw new Error('Container was expected to be available in useEffect. This is a bug.');
      // We only want to create or destroy on mount, hot reload or tag change.
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [tag]);
    // With React 17 we should be able to just use jsx runtime.
    return /*#__PURE__*/React.createElement(tag || 'div', __assign(__assign({}, elementProps), {
      // such as style...
      ref: ref
    }));
  });
}

// Explicit name and type are needed for storybook
var Chart = wrap(Chart$1);

// Explicit name and type are needed for storybook
var MarkdownText = wrap(MarkdownText$1);

// Explicit name and type are needed for storybook
var KpiCard = wrap(KpiCard$1);

// Explicit name and type are needed for storybook
var ChoroplethGeoJson = wrap(ChoroplethGeoJson$1);

// Explicit name and type are needed for storybook
var ChoroplethVectorTiles = wrap(ChoroplethVectorTiles$1);

// Explicit name and type are needed for storybook
var ChoroplethSvg = wrap(ChoroplethSvg$1);

// Explicit name and type are needed for Storybook
var PoiMap = wrap(PoiMap$1);

export { Chart, ChoroplethGeoJson, ChoroplethSvg, ChoroplethVectorTiles, KpiCard, MarkdownText, PoiMap };
//# sourceMappingURL=index.es.js.map
