/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { i as isString_1 } from '../../../_chunks/dep-876fb3ec.mjs';
import { i as isNull_1 } from '../../../_chunks/dep-ed5ebb35.mjs';
import { t as tinycolor } from '../../../_chunks/dep-9ac6ced8.mjs';
import '../../../_chunks/dep-eba0ea6e.mjs';
import '../../../_chunks/dep-469b4ae0.mjs';
import '../../../_chunks/dep-255303ba.mjs';
import '../../../_chunks/dep-b57180d0.mjs';
import '../../../_chunks/dep-c8d7b6b4.mjs';
import '../../../_chunks/dep-3da99438.mjs';

var combineRegExp = function combineRegExp(regexpList, flags) {
  var source = "";
  for (var i = 0; i < regexpList.length; i++) {
    if (isString_1(regexpList[i])) {
      source += regexpList[i];
    } else {
      source += regexpList[i].source;
    }
  }
  return new RegExp(source, flags);
};
var generateRegExp = function generateRegExp() {
  var searchFlags = "gi";
  var rAngle = /(?:[+-]?\d*\.?\d+)(?:deg|grad|rad|turn)/;
  var rSideCornerCapture = /to\s+((?:(?:left|right|top|bottom)(?:\s+(?:top|bottom|left|right))?))/;
  var rComma = /\s*,\s*/;
  var rColorHex = /#(?:[a-f0-9]{6}|[a-f0-9]{3})/;
  var rDigits3 = /\(\s*(?:\d{1,3}\s*,\s*){2}\d{1,3}\s*\)/;
  var rDigits4 = /\(\s*(?:\d{1,3}\s*,\s*){2}\d{1,3}\s*,\s*\d*\.?\d+\)/;
  var rValue = /(?:[+-]?\d*\.?\d+)(?:%|[a-z]+)?/;
  var rKeyword = /[_a-z-][_a-z0-9-]*/;
  var rColor = combineRegExp(["(?:", rColorHex, "|", "(?:rgb|hsl)", rDigits3, "|", "(?:rgba|hsla)", rDigits4, "|", rKeyword, ")"], "");
  var rColorStop = combineRegExp([rColor, "(?:\\s+", rValue, "(?:\\s+", rValue, ")?)?"], "");
  var rColorStopList = combineRegExp(["(?:", rColorStop, rComma, ")*", rColorStop], "");
  var rLineCapture = combineRegExp(["(?:(", rAngle, ")|", rSideCornerCapture, ")"], "");
  var rGradientSearch = combineRegExp(["(?:(", rLineCapture, ")", rComma, ")?(", rColorStopList, ")"], searchFlags);
  var rColorStopSearch = combineRegExp(["\\s*(", rColor, ")", "(?:\\s+", "(", rValue, "))?", "(?:", rComma, "\\s*)?"], searchFlags);
  return {
    gradientSearch: rGradientSearch,
    colorStopSearch: rColorStopSearch
  };
};
var parseGradient = function parseGradient(regExpLib, input) {
  var result;
  var matchColorStop;
  var stopResult;
  regExpLib.gradientSearch.lastIndex = 0;
  var matchGradient = regExpLib.gradientSearch.exec(input);
  if (!isNull_1(matchGradient)) {
    result = {
      original: matchGradient[0],
      colorStopList: []
    };
    if (matchGradient[1]) {
      result.line = matchGradient[1];
    }
    if (matchGradient[2]) {
      result.angle = matchGradient[2];
    }
    if (matchGradient[3]) {
      result.sideCorner = matchGradient[3];
    }
    regExpLib.colorStopSearch.lastIndex = 0;
    matchColorStop = regExpLib.colorStopSearch.exec(matchGradient[4]);
    while (!isNull_1(matchColorStop)) {
      stopResult = {
        color: matchColorStop[1]
      };
      if (matchColorStop[2]) {
        stopResult.position = matchColorStop[2];
      }
      result.colorStopList.push(stopResult);
      matchColorStop = regExpLib.colorStopSearch.exec(matchGradient[4]);
    }
  }
  return result;
};
var REGEXP_LIB = generateRegExp();
var REG_GRADIENT = /.*gradient\s*\(((?:\([^)]*\)|[^)(]*)*)\)/gim;
var isGradientColor = function isGradientColor(input) {
  REG_GRADIENT.lastIndex = 0;
  return REG_GRADIENT.exec(input);
};
var sideCornerDegreeMap = {
  top: 0,
  right: 90,
  bottom: 180,
  left: 270,
  "top left": 225,
  "left top": 225,
  "top right": 135,
  "right top": 135,
  "bottom left": 315,
  "left bottom": 315,
  "bottom right": 45,
  "right bottom": 45
};
var parseGradientString = function parseGradientString(input) {
  var match = isGradientColor(input);
  if (!match) {
    return false;
  }
  var gradientColors = {
    points: [],
    degree: 0
  };
  var result = parseGradient(REGEXP_LIB, match[1]);
  if (result.original.trim() !== match[1].trim()) {
    return false;
  }
  var points = result.colorStopList.map(function (_ref) {
    var color = _ref.color,
      position = _ref.position;
    var point = /* @__PURE__ */Object.create(null);
    point.color = tinycolor(color).toRgbString();
    point.left = parseFloat(position);
    return point;
  });
  gradientColors.points = points;
  var degree = parseInt(result.angle, 10);
  if (Number.isNaN(degree)) {
    degree = sideCornerDegreeMap[result.sideCorner] || 90;
  }
  gradientColors.degree = degree;
  return gradientColors;
};

export { parseGradientString as default, isGradientColor, parseGradientString };
//# sourceMappingURL=gradient.mjs.map
