// @ts-ignore
import { toCSS, toJSON } from 'cssjson';
function getComputePropertyValue(node, property) {
  var nativeNode = node.getDOMNode();
  if (!nativeNode) return null;
  try {
    return window.getComputedStyle(nativeNode, null).getPropertyValue(property);
  } catch (e) {
    console.error(e);
    return null;
  }
}

/**
 * 获取提示属性值
 * @param field
 * @param property
 */
export function getPlaceholderPropertyValue(field, property) {
  var propertyValue = getComputePropertyValue(field.getNode(), toLine(property));
  if (propertyValue != 'auto' && propertyValue != '') {
    if (property != 'backgroundColor') {
      // @ts-ignore
      return removeUnit(propertyValue);
    } else {
      // @ts-ignore
      return hexify(propertyValue);
    }
  }
  return propertyValue;
}
export function removeUnit(value) {
  if (value != undefined && value != null) {
    return parseInt(value);
  }
  return null;
}
export function addUnit(value, unit) {
  if (value != undefined && value != null) {
    return value + unit;
  } else {
    return null;
  }
}
export function isEmptyValue(value) {
  if (value == undefined || value == null) {
    return true;
  }
  return false;
}

/**
 * 将驼峰写法改成xx-xx的css命名写法
 * @param styleKey
 */
export function toLine(styleKey) {
  return styleKey.replace(/([A-Z])/g, '-$1').toLowerCase();
}
export function toHump(name) {
  return name.replace(/\-(\w)/g, function (all, letter) {
    return letter.toUpperCase();
  });
}

/**
 * rgba转16进制
 * @param color
 */
export function hexify(color) {
  var values = color.replace(/rgba?\(/, '').replace(/\)/, '').replace(/[\s+]/g, '').split(',');
  var a = parseFloat(values[3]);
  var r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255);
  var g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255);
  var b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
  return "#" + ("0" + r.toString(16)).slice(-2) + ("0" + g.toString(16)).slice(-2) + ("0" + b.toString(16)).slice(-2);
}
export function parseToCssCode(styleData) {
  var parseStyleData = {};
  for (var styleKey in styleData) {
    // @ts-ignore
    parseStyleData[toLine(styleKey)] = styleData[styleKey];
  }
  var cssJson = {
    children: {
      '#main': {
        children: {},
        attributes: parseStyleData
      }
    }
  };
  return toCSS(cssJson);
}
export function parseToStyleData(cssCode) {
  var styleData = {};
  try {
    var _cssJson$children, _cssJson$children$Ma;
    var cssJson = toJSON(cssCode);
    var cssJsonData = cssJson === null || cssJson === void 0 ? void 0 : (_cssJson$children = cssJson.children) === null || _cssJson$children === void 0 ? void 0 : (_cssJson$children$Ma = _cssJson$children['#main']) === null || _cssJson$children$Ma === void 0 ? void 0 : _cssJson$children$Ma.attributes;
    for (var key in cssJsonData) {
      // @ts-ignore
      styleData[toHump(key)] = cssJsonData[key];
    }
    // 转化key
  } catch (e) {
    // @ts-ignore
    console.error(e.message);
  }
  return styleData;
}
// 多参数样式解析单个参数
export var parseValue = function parseValue(styleValue, valueIndex) {
  if (!styleValue) return;
  var styleArray = (styleValue === null || styleValue === void 0 ? void 0 : styleValue.split(' ')) || [];
  var value = styleArray[valueIndex];
  var unifiedValue = unifyValue(value);
  return unifiedValue === 'auto' ? null : unifiedValue;
};

// 去除一下单位
export var unifyValue = function unifyValue(value) {
  if (/^-?\d+px$/.test(value)) {
    return value.replace('px', '');
  }
  return value;
};

// 规范手动输入的css样式
export var unifyStyle = function unifyStyle(value) {
  if (!value) return;
  // 首部空格去除
  if (value.substring(0, 1) === ' ') {
    value.replace(/\s+/, '');
  }
  // 多属性间重复字符串去除
  value.replace(/\s+/g, ' ');
  return value;
};
export var getUnit = function getUnit(value) {
  if (typeof value !== 'string') return '';
  if (value != undefined && value != null) {
    return value.replace(/^-?[0-9]\d*/g, '');
  }
};
export function isCssVarBind(value) {
  if (typeof value === 'string') {
    return /var\(/.test(value);
  }
}