"use strict";

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

var keyWordToPercent = function keyWordToPercent(v) {
  if (v === 'left' || v === 'top') {
    return 0;
  } else if (v === 'right' || v === 'bottom') {
    return 100;
  } else if (v === 'center') {
    return 50;
  } else if (v.endsWith('%')) {
    return parseFloat(v);
  } else {
    return v;
  }
};

var getValue = function getValue(s, length) {
  if (s.endsWith('px')) {
    return parseFloat(s);
  } else {
    s = keyWordToPercent(s);
    var v = length * (s / 100);
    return v;
  }
};

var normalize = function normalize(a) {
  if (a[0] === 'bottom' || a[0] === 'top' || a[1] === 'left' || a[1] === 'right') {
    return [a[1], a[0]];
  } else {
    return a;
  }
};
/**
 * Parse a transform origin string to x/y values.
 * @param {{width: number, height: number}} rect
 * @param {string} value
 * @returns {x:number, y:number}
 */


var parse = function parse(rect, value) {
  if (!value) {
    return {
      x: rect.width / 2,
      y: rect.height / 2
    };
  }

  var arr = value.split(' ');

  if (arr.length === 1) {
    //1 val
    var x = getValue(arr[0], rect.width);
    var y = getValue('50%', rect.height);
    return {
      x: x,
      y: y
    };
  } else if (arr.length === 2) {
    var sorted = normalize(arr);

    var _x = getValue(sorted[0], rect.width);

    var _y = getValue(sorted[1], rect.height);

    return {
      x: _x,
      y: _y
    };
  } else if (arr.length === 3) {
    throw new Error('transform-origin values with 3 fields not supported.');
  }
};

exports.parse = parse;
//# sourceMappingURL=transform-origin.js.map