import { MAX_COLOR_RGBA } from './consts.js';
import { hsv2hsl } from './hsv2hsl.js';
import { getColorFromRGBA } from './getColorFromRGBA.js';
import { hsv2rgb } from './hsv2rgb.js';
import { assign } from '@uifabric-vue/utilities';

var WhiteShadeTableBG = [0.027, 0.043, 0.082, 0.145, 0.184, 0.216, 0.349, 0.537];
var BlackTintTableBG = [0.537, 0.45, 0.349, 0.216, 0.184, 0.145, 0.082, 0.043];
var WhiteShadeTable = [0.537, 0.349, 0.216, 0.184, 0.145, 0.082, 0.043, 0.027];
var BlackTintTable = [0.537, 0.45, 0.349, 0.216, 0.184, 0.145, 0.082, 0.043];
var LumTintTable = [0.88, 0.77, 0.66, 0.55, 0.44, 0.33, 0.22, 0.11];
var LumShadeTable = [0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88];
var ColorTintTable = [0.96, 0.84, 0.7, 0.4, 0.12];
var ColorShadeTable = [0.1, 0.24, 0.44];
var LowLuminanceThreshold = 0.2;
var HighLuminanceThreshold = 0.8;
var Shade;
(function(Shade2) {
  Shade2[Shade2["Unshaded"] = 0] = "Unshaded";
  Shade2[Shade2["Shade1"] = 1] = "Shade1";
  Shade2[Shade2["Shade2"] = 2] = "Shade2";
  Shade2[Shade2["Shade3"] = 3] = "Shade3";
  Shade2[Shade2["Shade4"] = 4] = "Shade4";
  Shade2[Shade2["Shade5"] = 5] = "Shade5";
  Shade2[Shade2["Shade6"] = 6] = "Shade6";
  Shade2[Shade2["Shade7"] = 7] = "Shade7";
  Shade2[Shade2["Shade8"] = 8] = "Shade8";
})(Shade || (Shade = {}));
function isValidShade(shade) {
  return typeof shade === "number" && shade >= Shade.Unshaded && shade <= Shade.Shade8;
}
function _isBlack(color) {
  return color.r === 0 && color.g === 0 && color.b === 0;
}
function _isWhite(color) {
  return color.r === MAX_COLOR_RGBA && color.g === MAX_COLOR_RGBA && color.b === MAX_COLOR_RGBA;
}
function _darken(hsv, factor) {
  return {
    h: hsv.h,
    s: hsv.s,
    v: _clamp(hsv.v - hsv.v * factor)
  };
}
function _lighten(hsv, factor) {
  return {
    h: hsv.h,
    s: _clamp(hsv.s - hsv.s * factor),
    v: _clamp(hsv.v + (100 - hsv.v) * factor)
  };
}
function _clamp(n, min, max) {
  return n;
}
function isDark(color) {
  return hsv2hsl(color.h, color.s, color.v).l < 50;
}
function getShade(color, shade, isInverted) {
  if (isInverted === void 0) {
    isInverted = false;
  }
  if (!color) {
    return null;
  }
  if (shade === Shade.Unshaded || !isValidShade(shade)) {
    return color;
  }
  var hsl = hsv2hsl(color.h, color.s, color.v);
  var hsv = { h: color.h, s: color.s, v: color.v };
  var tableIndex = shade - 1;
  var _soften = _lighten;
  var _strongen = _darken;
  if (isInverted) {
    _soften = _darken;
    _strongen = _lighten;
  }
  if (_isWhite(color)) {
    hsv = _darken(hsv, WhiteShadeTable[tableIndex]);
  } else if (_isBlack(color)) {
    hsv = _lighten(hsv, BlackTintTable[tableIndex]);
  } else if (hsl.l / 100 > HighLuminanceThreshold) {
    hsv = _strongen(hsv, LumShadeTable[tableIndex]);
  } else if (hsl.l / 100 < LowLuminanceThreshold) {
    hsv = _soften(hsv, LumTintTable[tableIndex]);
  } else {
    if (tableIndex < ColorTintTable.length) {
      hsv = _soften(hsv, ColorTintTable[tableIndex]);
    } else {
      hsv = _strongen(hsv, ColorShadeTable[tableIndex - ColorTintTable.length]);
    }
  }
  return getColorFromRGBA(assign(hsv2rgb(hsv.h, hsv.s, hsv.v), { a: color.a }));
}
function getBackgroundShade(color, shade, isInverted) {
  if (isInverted === void 0) {
    isInverted = false;
  }
  if (!color) {
    return null;
  }
  if (shade === Shade.Unshaded || !isValidShade(shade)) {
    return color;
  }
  var hsv = { h: color.h, s: color.s, v: color.v };
  var tableIndex = shade - 1;
  if (!isInverted) {
    hsv = _darken(hsv, WhiteShadeTableBG[tableIndex]);
  } else {
    hsv = _lighten(hsv, BlackTintTableBG[BlackTintTable.length - 1 - tableIndex]);
  }
  return getColorFromRGBA(assign(hsv2rgb(hsv.h, hsv.s, hsv.v), { a: color.a }));
}
function getContrastRatio(color1, color2) {
  function _getThing(x) {
    if (x <= 0.03928) {
      return x / 12.92;
    } else {
      return Math.pow((x + 0.055) / 1.055, 2.4);
    }
  }
  var r1 = _getThing(color1.r / MAX_COLOR_RGBA);
  var g1 = _getThing(color1.g / MAX_COLOR_RGBA);
  var b1 = _getThing(color1.b / MAX_COLOR_RGBA);
  var L1 = 0.2126 * r1 + 0.7152 * g1 + 0.0722 * b1;
  L1 += 0.05;
  var r2 = _getThing(color2.r / MAX_COLOR_RGBA);
  var g2 = _getThing(color2.g / MAX_COLOR_RGBA);
  var b2 = _getThing(color2.b / MAX_COLOR_RGBA);
  var L2 = 0.2126 * r2 + 0.7152 * g2 + 0.0722 * b2;
  L2 += 0.05;
  return L1 / L2 > 1 ? L1 / L2 : L2 / L1;
}

export { Shade, getBackgroundShade, getContrastRatio, getShade, isDark, isValidShade };
