"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.log = exports.set = exports.getResolvedProps = exports.getThemeProps = exports.getThemeObject = exports.get = exports.init = exports.COLOR_SCHEME_MAP = exports.PSEUDO_PROP_COMPONENT_MAP = exports.pseudoPropStateMap = exports.resolvedStyledMap = void 0;

var _lodash = require("lodash");

var _isEmptyObj = require("../utils/isEmptyObj");

var _theme = require("../theme");

var _styled = require("../utils/styled");

// import type { ColorMode } from './color-mode';
// Adding Map for storing the props and style for the styled component
let resolvedStyledMap = {};
exports.resolvedStyledMap = resolvedStyledMap;
const pseudoPropStateMap = {
  _disabled: 'isDisabled',
  _focusVisible: 'isFocusVisible',
  _focus: 'isFocused',
  _hover: 'isHovered',
  _pressed: 'isPressed',
  _checked: 'isChecked',
  _loading: 'isLoading',
  _invalid: 'isInvalid',
  _reversed: 'isReversed'
};
exports.pseudoPropStateMap = pseudoPropStateMap;
const PSEUDO_PROP_COMPONENT_MAP = {
  _spinner: 'Spinner',
  _stack: 'Stack',
  _text: 'Text',
  _icon: 'Icon',
  _checkbox: 'Checkbox',
  _radio: 'Radio',
  _pressable: 'Pressable' // _label: 'Text',
  // _input: 'Input',
  // _slide: 'Slide',
  // _backdropFade: 'BackdropFade',
  // _fade: 'Fade',

};
exports.PSEUDO_PROP_COMPONENT_MAP = PSEUDO_PROP_COMPONENT_MAP;
const COLOR_SCHEME_MAP = {
  Button: true,
  ButtonGroup: true,
  IconButton: true,
  Checkbox: true,
  Radio: true,
  Alert: true,
  Badge: true,
  CircularProgress: true,
  Fab: true,
  Modal: true,
  Progress: true,
  Switch: true,
  Tag: true,
  Slider: true,
  SliderThumb: true,
  // SliderTrack: true,
  SliderFilledTrack: true
};
exports.COLOR_SCHEME_MAP = COLOR_SCHEME_MAP;

const init = inputResolvedStyledMap => {
  // resolvedStyledMap = new Map();
  if (inputResolvedStyledMap) {
    exports.resolvedStyledMap = resolvedStyledMap = inputResolvedStyledMap;
  }

  if (process.env.NODE_ENV === 'development') {
    //@ts-ignore
    window['resolvedStyledMap'] = resolvedStyledMap; //@ts-ignore
  }
};

exports.init = init;

const get = key => {
  // console.log(key, 'key here 111');
  // console.log(resolvedStyledMap[key], 'key here 111');
  return resolvedStyledMap[key];
};

exports.get = get;

const getThemeObject = (componentName, colorMode, state) => {
  const styleObj = resolvedStyledMap[componentName];

  if (!styleObj) {
    return {};
  } // Theme style


  let styleSheet = styleObj[colorMode]; // state style

  const stateStyles = getPseudoStateStyles(componentName, state);
  (0, _lodash.forEach)(stateStyles, stateStyleObj => {
    if (stateStyleObj[colorMode]) {
      styleSheet = styleSheet.concat(stateStyleObj[colorMode]);
    }
  });
  const unResolvedPropsArray = (0, _lodash.map)(styleSheet, 'unResolvedProps');
  let unResolvedProps = {};

  for (const props of unResolvedPropsArray) {
    unResolvedProps = { ...unResolvedProps,
      ...props
    };
  }

  const restDefaultPropsArray = (0, _lodash.map)(styleSheet, 'restDefaultProps');
  let restDefaultProps = {};

  for (const props of restDefaultPropsArray) {
    restDefaultProps = { ...restDefaultProps,
      ...props
    };
  }

  const styleFromPropsArray = (0, _lodash.map)(styleSheet, 'styleFromProps');
  let styleFromProps = {};

  for (const props of styleFromPropsArray) {
    styleFromProps = { ...styleFromProps,
      ...props
    };
  }

  const internalPseudoPropsArray = (0, _lodash.map)(styleSheet, 'internalPseudoProps');
  let internalPseudoProps = {};

  for (const props of internalPseudoPropsArray) {
    internalPseudoProps = { ...internalPseudoProps,
      ...props
    };
  }

  return {
    style: (0, _lodash.map)(styleSheet, 'style'),
    unResolvedProps: unResolvedProps,
    styleFromProps: styleFromProps,
    restDefaultProps: restDefaultProps,
    internalPseudoProps: internalPseudoProps
  };
};

exports.getThemeObject = getThemeObject;

const getComponentNameKeyFromProps = (componentName, {
  variant,
  colorScheme
} = {}) => {
  var _componentTheme$defau;

  let componentKeyName = componentName;
  const componentTheme = (0, _lodash.get)(_theme.theme, "components.".concat(componentName), {});
  const colorSchemeKey = colorScheme || ((_componentTheme$defau = componentTheme.defaultProps) === null || _componentTheme$defau === void 0 ? void 0 : _componentTheme$defau.colorScheme);

  if (colorSchemeKey && variant) {
    componentKeyName = "".concat(componentName, ".").concat(colorSchemeKey, ".").concat(variant);
  } else if (variant) {
    componentKeyName = "".concat(componentName, ".").concat(variant);
  } else if (colorSchemeKey) {
    componentKeyName = "".concat(componentName, ".").concat(colorSchemeKey);
  }

  return componentKeyName;
}; // const get


const getThemeProps = (inputComponentKeyName, config, state, props = {}) => {
  const componentNames = inputComponentKeyName.split('.');
  const rootComponentName = componentNames[0];
  const pseudoComponentKeyName = componentNames[1];
  let componentKeyName = rootComponentName;
  componentKeyName = getComponentNameKeyFromProps(rootComponentName, props);

  if (pseudoComponentKeyName) {
    componentKeyName = "".concat(componentKeyName, ".").concat(pseudoComponentKeyName);
  }

  let themeObj = getThemeObject(componentKeyName, config.colorMode, state);

  if ((0, _isEmptyObj.isEmptyObj)(themeObj)) {
    (0, _styled.updateComponentThemeMap)(inputComponentKeyName, {}, config, true, props);
    themeObj = getThemeObject(componentKeyName, config.colorMode, state);
  }

  if (!(0, _isEmptyObj.isEmptyObj)(themeObj) && props.size) {
    var _sizeThemeObj, _themeObj, _sizeThemeObj2;

    let componentKeyNameForSize = "".concat(rootComponentName, ".").concat(props.size);

    if (pseudoComponentKeyName) {
      componentKeyNameForSize = "".concat(componentKeyNameForSize, ".").concat(pseudoComponentKeyName);
    }

    let sizeThemeObj = getThemeObject("".concat(componentKeyNameForSize), config.colorMode, state);

    if ((0, _isEmptyObj.isEmptyObj)(sizeThemeObj)) {
      (0, _styled.updateComponentThemeMap)(rootComponentName, {}, config, true, props);
      sizeThemeObj = getThemeObject(componentKeyNameForSize, config.colorMode, state);
    }

    const mergedThemeObj = {
      style: (_sizeThemeObj = sizeThemeObj) !== null && _sizeThemeObj !== void 0 && _sizeThemeObj.style ? [...((_themeObj = themeObj) === null || _themeObj === void 0 ? void 0 : _themeObj.style), ...((_sizeThemeObj2 = sizeThemeObj) === null || _sizeThemeObj2 === void 0 ? void 0 : _sizeThemeObj2.style)] : themeObj.style,
      styleFromProps: (0, _lodash.merge)({}, themeObj.styleFromProps, sizeThemeObj.styleFromProps),
      unResolvedProps: (0, _lodash.merge)({}, themeObj.unResolvedProps, sizeThemeObj.unResolvedProps),
      internalPseudoProps: (0, _lodash.merge)({}, themeObj.internalPseudoProps, sizeThemeObj.internalPseudoProps),
      restDefaultProps: { ...themeObj.restDefaultProps,
        ...sizeThemeObj.restDefaultProps
      }
    };
    themeObj = mergedThemeObj;
  }

  return themeObj;
};

exports.getThemeProps = getThemeProps;

const getResolvedProps = (key, colorMode) => {
  const styleObj = resolvedStyledMap[key];

  if (!colorMode || !styleObj) {
    return null;
  }

  return styleObj[colorMode]['styleFromProps'];
};

exports.getResolvedProps = getResolvedProps;

const isValidStateKey = (stateKey, state) => {
  // console.log(stateKey, pseudoPropStateMap[stateKey], state, 'is valid');
  try {
    //@ts-ignore
    return state[pseudoPropStateMap[stateKey]];
  } catch (e) {
    return false;
  }
};

const isValidState = (key, state) => {
  // include only startWith("_")
  const stateKeys = key.split('.');
  const isValid = stateKeys.every(stateKey => {
    if (isValidStateKey(stateKey, state)) {
      return true;
    }

    return false;
  }); // console.log(isValid, 'valid here');

  return isValid;
};

const getPseudoStateStyles = (componentName, state) => {
  const styleObj = [];
  const stateStyleArray = [];
  const componentStates = get(componentName); // console.log(componentStates, '***** &&&&');

  for (const k in componentStates) {
    const value = componentStates[k]; // console.log(componentStates, k, componentName, 'value **&');
    //get for _hover, _checked

    if (k.startsWith('_')) {
      // const pseudoPropKey = k.slice(componentName.length + 1);
      stateStyleArray.push({
        key: k,
        value: value
      });
    }
  } // sort for specificity


  stateStyleArray.sort((obj1, obj2) => obj1.key.length - obj2.key.length);
  stateStyleArray.forEach(item => {
    if (isValidState(item.key, state)) {
      styleObj.push(item.value);
    }
  }); // console.log(styleObj, 'valid state *');

  return styleObj;
}; // export const getResolvedStyleSheet = (
//   componentName: string | Array<any>,
//   colorMode?: ColorMode,
//   state?: any,
//   variant?: any,
//   size?: any
// ) => {
//   if (typeof componentName !== 'string') {
//     componentName = componentName.filter((item) => item).join('.');
//     // console.log(componentName, 'component name &&&^');
//   } else {
//     if (variant && size) {
//       componentName = `${componentName}.${variant}.${size}`;
//     } else if (variant) {
//       componentName = `${componentName}.${variant}`;
//     } else if (size) {
//       componentName = `${componentName}.${size}`;
//     }
//   }
//   const styleObj: any = resolvedStyledMap.get(componentName);
//   // console.log(componentName, colorMode, styleObj, 'component name &&&&&');
//   if (!colorMode || !styleObj) {
//     return null;
//   }
//   // Theme style
//   const styleSheet = map(styleObj[colorMode], 'style');
//   // state style
//   const stateStyles = getPseudoStateStyles(componentName, state);
//   forEach(stateStyles, (stateStyleObj) => {
//     // console.log(
//     //   componentName,
//     //   stateStyleObj[colorMode].style,
//     //   map(stateStyleObj[colorMode], 'style'),
//     //   'hello component name )))'
//     // );
//     styleSheet.push(map(stateStyleObj[colorMode], 'style'));
//   });
//   return styleSheet;
// };


const set = (key, value, colorMode) => {
  const styledMap = resolvedStyledMap[key];

  if (!styledMap) {
    resolvedStyledMap[key] = {
      [colorMode]: [value]
    };
  } else {
    if (!styledMap[colorMode]) {
      styledMap[colorMode] = [];
    }

    styledMap[colorMode].push(value);
  }
};

exports.set = set;

const log = () => {// console.log(resolvedStyledMap);
};

exports.log = log;
//# sourceMappingURL=ResolvedStyleMap.js.map