<template>
  <td class="square-wrapper">
    <div class="square-inner">
      <div class="correlation-viz" :style="squareStyle"></div>
      <div class="value">
        <div v-show="showNumber">{{ Number(value).toFixed(2) }}</div>
      </div>
    </div>
  </td>
</template>

<script>
function correlationToPercent(correlation) {
  const map = (value, x1, y1, x2, y2) =>
    ((value - x1) * (y2 - x2)) / (y1 - x1) + x2;

  return map(Math.abs(correlation) * 100, 0, 100, 10, 100).toFixed() + "%";
}

export default {
  props: { value: Number, p: Number, scale: Function }, // -1 to 1
  data() {
    return {
      showNumber: true,
    };
  },
  computed: {
    squareStyle() {
      // .range(["#2ca02c", "#1f77b4", "#ff7f0e"]);
      return {
        height: correlationToPercent(this.value),
        width: correlationToPercent(this.value),
        backgroundColor: this.scale(this.value),
        filter: this.p > 0.05 ? "blur(3px)" : "none",
      };
    },
  },
};
</script>

<style scoped>
.value {
  position: absolute;
  filter: blur(0);
  color: white;
  text-shadow: 0.05em 0 black, 0 0.05em black, -0.05em 0 black, 0 -0.05em black,
    -0.05em -0.05em black, -0.05em 0.05em black, 0.05em -0.05em black,
    0.05em 0.05em black;
}

.correlation-viz {
  display: flex;
  justify-content: center;
  align-items: center;
}
.square-wrapper {
  margin: 1px;
  flex: 1 0 auto;
  position: relative;
}

.square-wrapper:after {
  content: "";
  float: left;
  display: block;
  padding-top: 100%;
}

.square-wrapper .square-inner {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>