/*
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,
  target
}) {
  const colors = flattenColorPalette(theme('colors')); //   gradient stop colors

  const fromStartColor = getRGB(colors.secondary);
  const viaStartColor = getRGB(colors.magenta);
  const toStartColor = getRGB(colors.primary); // Set CSS Varaiables

  const rootStyles = {
    ":root": {
      "--tw-gradient-direction": "0deg",
      "--tw-gradient-from-stop": "0%",
      "--tw-gradient-via-stop": "50%",
      "--tw-gradient-to-stop": "100%",
      "--tw-gradient-from-opacity": "1",
      "--tw-gradient-via-opacity": "1",
      "--tw-gradient-to-opacity": "1",
      "--tw-gradient-from": `rgba(${fromStartColor}, var(--tw-gradient-from-opacity))`,
      "--tw-gradient-via": `rgba(${viaStartColor}, var(--tw-gradient-via-opacity))`,
      "--tw-gradient-to": `rgba(${toStartColor}, var(--tw-gradient-to-opacity))`,
      "--tw-gradient-stops": `var(--tw-gradient-from) var(--tw-gradient-from-stop), var(--tw-gradient-to, rgba(${fromStartColor}, 0)) var(--tw-gradient-to-stop)`
    }
  }; // Gradient stop positions
  // Uses same values as opacity but as percentage not decimal.

  const fromStop = _.map(theme("gradientStopPosition"), (value, key) => {
    return {
      [`.${e(`from-stop-${key}`)}`]: {
        "--tw-gradient-from-stop": `${value}`
      }
    };
  });

  const viaStop = _.map(theme("gradientStopPosition"), (value, key) => {
    return {
      [`.${e(`via-stop-${key}`)}`]: {
        "--tw-gradient-via-stop": `${value}`
      }
    };
  });

  const toStop = _.map(theme("gradientStopPosition"), (value, key) => {
    return {
      [`.${e(`to-stop-${key}`)}`]: {
        "--tw-gradient-to-stop": `${value}`
      }
    };
  }); // Opacity


  const fromOpacity = _.map(theme("gradientOpacity"), (value, key) => {
    return {
      [`.${e(`from-opacity-${key}`)}`]: {
        "--tw-gradient-from-opacity": `${value}`
      }
    };
  });

  const viaOpacity = _.map(theme("gradientOpacity"), (value, key) => {
    return {
      [`.${e(`via-opacity-${key}`)}`]: {
        "--tw-gradient-via-opacity": `${value}`
      }
    };
  });

  const toOpacity = _.map(theme("gradientOpacity"), (value, key) => {
    return {
      [`.${e(`to-opacity-${key}`)}`]: {
        "--tw-gradient-to-opacity": `${value}`
      }
    };
  }); // Colors
  // Fine control over the stop color and it's opacity


  const fromColor = _.map(colors, (value, key) => {
    if (value == 'currentColor') {
      return null;
    }

    let color = getRGB(value);
    return {
      [`.${e(`from-${key}`)}`]: {
        "--tw-gradient-from": `rgba(${color}, var(--tw-gradient-from-opacity))`,
        "--tw-gradient-stops": `var(--tw-gradient-from) var(--tw-gradient-from-stop), var(--tw-gradient-to, rgba(${color}, 0)) var(--tw-gradient-to-stop)`
      }
    };
  });

  const viaColor = _.map(colors, (value, key) => {
    if (value == 'currentColor') {
      return null;
    }

    let color = getRGB(value);
    return {
      [`.${e(`via-${key}`)}`]: {
        "--tw-gradient-via": `rgba(${color}, var(--tw-gradient-via-opacity))`,
        "--tw-gradient-stops": `var(--tw-gradient-from) var(--tw-gradient-from-stop), var(--tw-gradient-via) var(--tw-gradient-via-stop), var(--tw-gradient-to, rgba(${color}, 0)) var(--tw-gradient-to-stop)`
      }
    };
  });

  const toColor = _.map(colors, (value, key) => {
    if (value == 'currentColor') {
      return null;
    }

    let color = getRGB(value);
    return {
      [`.${e(`to-${key}`)}`]: {
        "--tw-gradient-to": `rgba(${color}, var(--tw-gradient-to-opacity))`
      }
    };
  });

  addBase([rootStyles]);
  addUtilities([fromStop, viaStop, toStop, fromOpacity, viaOpacity, toOpacity, fromColor, viaColor, toColor], variants("gradientColorStops"));
};