"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Signature = void 0;

var _preact = require("preact");

var _tryit = _interopRequireDefault(require("tryit"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

class AnimatedPath extends _preact.Component {
  constructor(props) {
    super(props);
    this.state = {
      firstRender: true,
      length: 0
    };
  }

  componentDidMount() {
    setTimeout(() => {
      let length = 0;
      (0, _tryit.default)(() => {
        // base could be null here because of delay
        length = Math.round(this.base.getTotalLength());
      });
      this.setState({
        length
      });
      setTimeout(() => {
        this.setState({
          firstRender: false
        });
      }, 100);
    }, 100);
  }

  render({
    path,
    animate
  }, {
    firstRender,
    length
  }) {
    if (!animate) {
      return (0, _preact.h)("path", {
        d: path,
        fill: "none",
        stroke: "black",
        "stroke-width": "5"
      });
    }

    return (0, _preact.h)("path", {
      key: length ? 'real' : 'fake',
      d: path,
      fill: "none",
      stroke: length ? 'black' : 'none',
      "stroke-width": "5",
      style: {
        transitionDuration: '1s',
        transitionDelay: '.25s',
        transitionProperty: 'stroke-dashoffset',
        strokeDashoffset: firstRender ? length : 0,
        strokeDasharray: length
      }
    });
  }

}

const getCenteredViewbox = path => {
  const targetWidth = 660;
  const cleaned = path.replace(/[^\d,. ]/g, '').trim();
  const parts = cleaned.split(' ');
  let maxX = 0;
  let minX = targetWidth;

  for (const part of parts) {
    const [x, y] = part.split(',').map(Number);

    if (isNaN(x) || isNaN(y)) {
      continue;
    }

    maxX = Math.max(maxX, x);
    minX = Math.min(minX, x);
  }

  const width = maxX - minX;
  const centerX = minX + (width || 2) / 2;
  const realCenter = targetWidth / 2;
  const centerDiffX = -1 * (realCenter - centerX);
  return "".concat(centerDiffX || 0, " 0 ").concat(targetWidth, " 300");
};

const Signature = (_ref) => {
  let {
    path,
    width = '100',
    animate
  } = _ref,
      rest = _objectWithoutProperties(_ref, ["path", "width", "animate"]);

  return (0, _preact.h)("svg", _extends({
    width: width,
    height: width / 2,
    viewBox: getCenteredViewbox(path)
  }, rest), (0, _preact.h)(AnimatedPath, {
    key: path.slice(0, 10),
    path: path,
    animate: animate
  }));
};

exports.Signature = Signature;