import postcss from "../../postcss.config";
import store from "@/store";

export function pxToVw(size) {
  return (
    (100 * size) / postcss.plugins["postcss-px-to-viewport"].viewportWidth +
    "vw"
  );
}

export function handleColor(data) {
  const leftColor = store.getters && store.getters.leftColor;
  const rightTopColor = store.getters && store.getters.rightTopColor;

  if (checkType(data) == "array") {
    data.map((item) => {
      if (!item.color) {
        item.leftColor = "#7b75ff";
        item.rightTopColor = "#7b75ff";
        return;
      }
      for (let key in leftColor) {
        if (item.color == key) {
          item.leftColor = leftColor[key];
          item.rightTopColor = rightTopColor[key];
        }
      }
    });
    return data;
  } else if (checkType(data) == "object") {
    if (!data.color) {
      data.leftColor = "#7b75ff";
      data.rightTopColor = "#7b75ff";
      return;
    }
    for (let key in leftColor) {
      if (data.color == key) {
        data.leftColor = leftColor[key];
        data.rightTopColor = rightTopColor[key];
      }
    }
    return data;
  } else {
    return data;
  }
}

function checkType(value) {
  if (Array.isArray(value)) {
    return "array";
  } else if (value !== null && typeof value === "object") {
    return "object";
  } else {
    return "neither";
  }
}
