// FitScale — rend un composant Figma à taille native et le met à l'échelle
// pour tenir dans `width` px (mesure au montage, transform: scale).
(function () {
  const h = React.createElement;
  function FitScale({ width, children }) {
    const ref = React.useRef(null);
    const [dims, setDims] = React.useState(null);
    React.useLayoutEffect(() => {
      const el = ref.current && ref.current.firstElementChild;
      if (el) {
        const w = el.offsetWidth, hh = el.offsetHeight;
        if (w && hh) setDims([w, hh]);
      }
    }, []);
    const scale = dims ? width / dims[0] : 0;
    return h('div', {
      style: {
        width,
        height: dims ? Math.round(dims[1] * scale) : Math.round(width * 0.4),
        position: 'relative',
        overflow: 'hidden',
      },
    },
      h('div', {
        ref,
        style: {
          position: 'absolute', left: 0, top: 0,
          transform: 'scale(' + scale + ')', transformOrigin: '0 0',
          visibility: dims ? 'visible' : 'hidden',
        },
      }, children));
  }
  window.FitScale = FitScale;
})();
