"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.visibleStyleFunction = exports.typographyStyleFunction = exports.transformStyleFunction = exports.transformColorValue = exports.textStyleFunctions = exports.textShadowStyleFunction = exports.spacingStyleFunction = exports.shadowStyleFunction = exports.positionStyleFunction = exports.opacityStyleFunction = exports.layoutStyleFunction = exports.createStyleFunction = exports.colorStyleFunction = exports.boxStyleFunctions = exports.borderStyleFunction = exports.backgroundColorStyleFunction = exports.allStyleFunctions = void 0;
var _typeHelpers = require("../utils/type-helpers");
var _styleProperties = require("./style-properties");
var _responsiveHelpers = require("./responsive-helpers");
/**
 * Creates a style function container that can be used to create a style function.
 * @param property - The name of the property to create a style function for.
 * @param transform - An optional transformation function to apply to the value of the property.
 * @param styleProperty - An optional name of the style property to use instead of the property name.
 * @param themeKey - An optional name of the theme key to use for the property value.
 * @returns A style function container that can be used to create a style function.
 */
const createStyleFunction = ({
  property,
  transform,
  styleProperty,
  themeKey
}) => {
  const styleProp = styleProperty ?? property.toString();

  /**
   * A style function that returns a style object with the given property and value.
   * @param props - The props object to get the property value from.
   * @param theme - The theme object to get the theme value from.
   * @param dimensions - The dimensions object to get the screen size from.
   * @returns A style object with the given property and value.
   */
  const func = (props, {
    theme,
    colorMode,
    dimensions
  }) => {
    // Initial value is the raw prop value
    let value = (0, _responsiveHelpers.isResponsiveObjectValue)(props[property], theme) ? (0, _responsiveHelpers.getValueForScreenSize)({
      responsiveValue: props[property],
      breakpoints: theme.breakpoints,
      dimensions
    }) : props[property];

    // Transform the value if transformation function exists
    if (transform) value = transform(value, colorMode);

    // Check if this value refers to a key in the theme config
    if ((0, _typeHelpers.isThemeKey)(theme, themeKey)) {
      if (value) {
        let themeTokenValue = theme[themeKey][value];

        // For color palettes with multiple shades
        if (themeKey === "palette") {
          if (typeof value === "string" && value.match(/^[a-z]+\.\d+$/)) {
            themeTokenValue = (0, _typeHelpers.getNestedObject)(theme, [themeKey, value.split(".")[0], value.split(".")[1]]);
          }
        }

        // Apply the value
        value = themeTokenValue ?? value;
      }
    }
    if (value === undefined) return {};
    if (styleProperty !== "skip") {
      return {
        [styleProp]: value
      };
    } else {
      return value;
    }
  };
  return {
    property,
    themeKey,
    func
  };
};

/**
 * Transforms a color value based on the current color mode.
 * @param value - The color value to transform.
 * @returns The transformed color value.
 */
exports.createStyleFunction = createStyleFunction;
const transformColorValue = (value, colorMode) => {
  // Color Mode color provided
  if (typeof value === "object") {
    if (colorMode === "light") return value.light;
    return value.dark;
  }
  return value;
};
exports.transformColorValue = transformColorValue;
const backgroundColorStyleFunction = exports.backgroundColorStyleFunction = [createStyleFunction({
  property: "backgroundColor",
  themeKey: "palette",
  transform: transformColorValue
}), createStyleFunction({
  property: "bgColor",
  styleProperty: "backgroundColor",
  themeKey: "palette",
  transform: transformColorValue
})];
const colorStyleFunction = exports.colorStyleFunction = createStyleFunction({
  property: "color",
  themeKey: "palette",
  transform: transformColorValue
});
const opacityStyleFunction = exports.opacityStyleFunction = createStyleFunction({
  property: "opacity"
});
const visibleStyleFunction = exports.visibleStyleFunction = createStyleFunction({
  property: "visible",
  styleProperty: "opacity",
  transform: value => value !== undefined ? value === false ? 0 : 1 : undefined
});
const typographyStyleFunction = exports.typographyStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.typographyProperties).map(property => {
  return createStyleFunction({
    property
  });
}), createStyleFunction({
  property: "letterSpacing",
  themeKey: "letterSpacings"
}), createStyleFunction({
  property: "fontFamily",
  themeKey: "fonts"
}), createStyleFunction({
  property: "fontWeight",
  themeKey: "fontWeights"
}), createStyleFunction({
  property: "fontSize",
  themeKey: "fontSizes"
}), createStyleFunction({
  property: "lineHeight",
  themeKey: "lineHeights"
})];
const layoutStyleFunction = exports.layoutStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.layoutProperties).map(property => {
  return createStyleFunction({
    property
  });
}), ...(0, _typeHelpers.getKeys)(_styleProperties.layoutPropertiesShorthand).map(property => {
  const styleProperty = _styleProperties.layoutPropertiesShorthand[property];
  return createStyleFunction({
    property,
    styleProperty
  });
})];
const transformStyleFunction = exports.transformStyleFunction = (0, _typeHelpers.getKeys)(_styleProperties.transformProperties).map(property => {
  return createStyleFunction({
    property
  });
});
const spacingStyleFunction = exports.spacingStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.spacingProperties).map(property => {
  return createStyleFunction({
    property,
    themeKey: "spacing"
  });
}), ...(0, _typeHelpers.getKeys)(_styleProperties.spacingPropertiesShorthand).map(property => {
  const styleProperty = _styleProperties.spacingPropertiesShorthand[property];
  return createStyleFunction({
    property,
    styleProperty,
    themeKey: "spacing"
  });
})];
const positionStyleFunction = exports.positionStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.positionProperties).map(property => {
  return createStyleFunction({
    property
  });
}), createStyleFunction({
  property: "zIndex",
  themeKey: "zIndices"
})];
const borderStyleFunction = exports.borderStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.borderProperties).map(property => {
  return createStyleFunction({
    property
  });
}), ...(0, _typeHelpers.getKeys)(_styleProperties.borderColorProperties).map(property => {
  return createStyleFunction({
    property,
    themeKey: "palette",
    transform: transformColorValue
  });
}), ...(0, _typeHelpers.getKeys)(_styleProperties.borderRadiusProperties).map(property => {
  return createStyleFunction({
    property,
    themeKey: "borderRadii"
  });
})];
const shadowStyleFunction = exports.shadowStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.shadowProperties).map(property => {
  return createStyleFunction({
    property
  });
}), createStyleFunction({
  property: "boxShadow",
  themeKey: "elevation",
  styleProperty: "skip"
}), createStyleFunction({
  property: "shadowColor",
  themeKey: "palette",
  transform: transformColorValue
})];
const textShadowStyleFunction = exports.textShadowStyleFunction = [...(0, _typeHelpers.getKeys)(_styleProperties.textShadowProperties).map(property => {
  return createStyleFunction({
    property
  });
}), createStyleFunction({
  property: "textShadowColor",
  themeKey: "palette",
  transform: transformColorValue
})];
const allStyleFunctions = exports.allStyleFunctions = [colorStyleFunction, opacityStyleFunction, visibleStyleFunction, ...backgroundColorStyleFunction, ...spacingStyleFunction, ...typographyStyleFunction, ...layoutStyleFunction, ...transformStyleFunction, ...positionStyleFunction, ...borderStyleFunction, ...shadowStyleFunction, ...textShadowStyleFunction];

// Component Specific Style function
const boxStyleFunctions = exports.boxStyleFunctions = [opacityStyleFunction, visibleStyleFunction, ...backgroundColorStyleFunction, ...layoutStyleFunction, ...transformStyleFunction, ...spacingStyleFunction, ...borderStyleFunction, ...shadowStyleFunction, ...positionStyleFunction];
const textStyleFunctions = exports.textStyleFunctions = [opacityStyleFunction, visibleStyleFunction, colorStyleFunction, ...backgroundColorStyleFunction, ...layoutStyleFunction, ...transformStyleFunction, ...typographyStyleFunction, ...spacingStyleFunction, ...textShadowStyleFunction, ...positionStyleFunction];

// PropTypes

// Component Style Prop
//# sourceMappingURL=style-functions.js.map