/*
Copyright 2021 Q-CTRL. All rights reserved.

Licensed under the Q-CTRL Terms of service (the "License"). Unauthorized
copying or use of this file, via any medium, is strictly prohibited.
Proprietary and confidential. You may not use this file except in compliance
with the License. You may obtain a copy of the License at

     https://q-ctrl.com/terms

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS. See the
License for the specific language.
*/
const _ = require("lodash");

const plugin = require("tailwindcss/plugin");

const {
  flattenColorPalette,
  getRGB
} = require("../../utilities/convertColors"); // Text orientation & writing mode


module.exports = function ({
  addBase,
  addUtilities,
  theme,
  e,
  variants
}) {
  const colors = flattenColorPalette(theme('colors'));
  const rootStyles = {
    ":root": {
      "--triangle-background-color": theme("colors.white"),
      "--triangle-opacity": "1",
      "--triangle-size": theme('triangleSize.md'),
      "--triangle-size-sm": `calc( var(--triangle-size) * 0.75)`,
      "--triangle-size-xs": `calc( var(--triangle-size) * 0.66)`
    },
    ".triangle, [class*='triangle-']": {
      display: "inline-block",
      width: 0,
      height: 0,
      borderStyle: "solid",
      borderWidth: "0 var(--triangle-size-xs) var(--triangle-size-sm) var(--triangle-size-xs)",
      borderColor: "transparent transparent var(--triangle-background-color) transparent"
    }
  };
  const triangleDirectionUtilities = {
    ".triangle-t": {
      borderWidth: "0 var(--triangle-size-xs) var(--triangle-size-sm) var(--triangle-size-xs)",
      borderColor: "transparent transparent var(--triangle-background-color) transparent"
    },
    ".triangle-r": {
      borderWidth: "var(--triangle-size-xs) 0 var(--triangle-size-xs) var(--triangle-size-sm)",
      borderColor: "transparent transparent transparent var(--triangle-background-color)"
    },
    ".triangle-b": {
      borderWidth: "var(--triangle-size-sm) var(--triangle-size-xs) 0 var(--triangle-size-xs)",
      borderColor: "var(--triangle-background-color) transparent transparent transparent"
    },
    ".triangle-l": {
      borderWidth: "var(--triangle-size-xs) var(--triangle-size-sm) var(--triangle-size-xs) 0",
      borderColor: "transparent var(--triangle-background-color) transparent transparent"
    },
    ".triangle-tr": {
      borderWidth: "0 var(--triangle-size) var(--triangle-size) 0",
      borderColor: "transparent var(--triangle-background-color) transparent transparent"
    },
    ".triangle-br": {
      borderWidth: "0 0 var(--triangle-size) var(--triangle-size)",
      borderColor: "transparent transparent var(--triangle-background-color) transparent"
    },
    ".triangle-bl": {
      borderWidth: "var(--triangle-size) 0 0 var(--triangle-size)",
      borderColor: "transparent transparent transparent var(--triangle-background-color)"
    },
    ".triangle-tl": {
      borderWidth: "var(--triangle-size) var(--triangle-size) 0 0",
      borderColor: "var(--triangle-background-color) transparent transparent transparent"
    }
  };

  const triangleSizeUtilities = _.map(theme("triangleSize"), (val, key) => {
    return {
      [`.${e(`triangle-${key}`)}`]: {
        "--triangle-size": `${val}`,
        "--triangle-size-sm": "calc( var(--triangle-size) * 0.75)",
        "--triangle-size-xs": "calc( var(--triangle-size) * 0.66)"
      }
    };
  });

  const triangleColorUtilities = _.map(colors, (value, key) => {
    if (key === 'current' || key === 'transparent') {
      return {
        [`.${e(`triangle-${key}`)}`]: {
          '--triangle-background-color': `${value}`
        }
      };
    }

    return {
      [`.${e(`triangle-${key}`)}`]: {
        '--triangle-background-color': `rgba(${getRGB(value)}, var(--triangle-opacity))`
      }
    };
  });

  const triangleOpacityUtilities = _.map(theme("opacity"), (value, key) => {
    return {
      [`.${e(`triangle-opacity-${key}`)}`]: {
        '--triangle-opacity': `${value}`
      }
    };
  });

  addBase([rootStyles]);
  addUtilities([triangleDirectionUtilities, triangleSizeUtilities, triangleColorUtilities, triangleOpacityUtilities], variants('triangleSize'));
};