import { isBorderRadiusTokenType, isDirectionalBorderRadiusType } from '../../../types/type-guard';
import { LogUtility } from '../../../utils/log';
import { BorderRadius } from '../../../constants/public-api';
import { ResponsiveCSSTextFormatter } from './responsive-css-text-formatter';
import { sortAsExistArray } from '../../../utils/array';
import { FeatureFlag } from '../../feature-control/feature-flag-controller';
const BORDER_RADIUS_DIRECTION_STYLE_MAP = {
  'top': ['border-top-left-radius', 'border-top-right-radius'],
  'right': ['border-top-right-radius', 'border-bottom-right-radius'],
  'bottom': ['border-bottom-left-radius', 'border-bottom-right-radius'],
  'left': ['border-top-left-radius', 'border-bottom-left-radius'],
  'top-left': ['border-top-left-radius'],
  'top-right': ['border-top-right-radius'],
  'bottom-left': ['border-bottom-left-radius'],
  'bottom-right': ['border-bottom-right-radius'],
};
/**
 * A class for format BorderRadiusType with BreakpointsForPrefixType to CSS text string
 *
 * @augments ResponsiveClassStringFormatter
 */
class ResponsiveBorderRadiusStyleFormatter extends ResponsiveCSSTextFormatter {
  /**
   * Given a border-radius type, return a string of CSS classes that can be used to apply the border-radius to an element
   *
   * @param {CornersType} input - CornersType
   * @returns {string} - A string of CSS text.
   */
  formatToken(input) {
    if (isBorderRadiusTokenType(input)) {
      return `border-radius:${BorderRadius[input]}px`;
    }
    else if (isDirectionalBorderRadiusType(input)) {
      return this.getDirectionKeys(input)
        .map((direction) => {
        return BORDER_RADIUS_DIRECTION_STYLE_MAP[direction]
          .map((item) => {
          return `${item}:${BorderRadius[input[direction]]}px`;
        })
          .join(';');
      })
        .join(';');
    }
    else {
      LogUtility.warn(`The border radius value ${input} is invalid, it must be a value of: `, BorderRadius);
      return '';
    }
  }
  /**
   * Get the corners object keys. Order the keys as exist array if the feature flag enabled.
   * The order affects the weight of the styles.
   *
   * @param {DirectionalBorderRadiusType} map - The corners input object.
   * @returns {string[]} - The input object keys.
   */
  getDirectionKeys(map) {
    const directionKeys = Object.keys(map);
    return FeatureFlag.isEnabled('VEGA_COMPONENT.CORNERS_USE_OLD_BEHAVIOR')
      ? sortAsExistArray(directionKeys, (direction) => map[direction], undefined, Object.keys(BorderRadius))
      : directionKeys;
  }
}
export default new ResponsiveBorderRadiusStyleFormatter();
