"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "_component", {
    enumerable: true,
    get: function() {
        return _component;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
require("focus-visible");
const _utils = require("./utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const _component = (componentConfig)=>{
    const { tag, base, name, intersections, invariants, responsive } = componentConfig;
    const staticVariants = (0, _utils.getStaticVariants)(componentConfig);
    const dynamicVariants = (0, _utils.getDynamicVariants)(componentConfig);
    const component = /*#__PURE__*/ (0, _react.forwardRef)((props, ref)=>{
        const { className, as, forwardedAs, responsive: responsiveProp, ...rest } = props;
        const variants = dynamicVariants(rest);
        const intersectionStyles = (0, _utils.getIntersectionStyles)(rest, intersections);
        const rootClassName = (0, _classnames.default)(className, base, ...staticVariants, ...variants, ...intersectionStyles, responsiveProp && responsive, invariants);
        // styled-components do it inside
        // filter props
        const baseProps = {};
        for(const key in rest){
            if (!(key in componentConfig.variations)) {
                baseProps[key] = rest[key];
            }
        }
        baseProps.ref = ref;
        // styled-components do it inside
        // add props that should be attr( disabled for example)
        const htmlAttrs = {};
        for(const key in componentConfig.variations || {}){
            if (key in rest) {
                const variant = componentConfig.variations[key];
                const { attrs } = variant;
                if (typeof attrs === 'boolean') {
                    htmlAttrs[key] = rest[key];
                }
            }
        }
        const Root = as || forwardedAs || tag;
        return /*#__PURE__*/ _react.default.createElement(Root, {
            className: rootClassName,
            ...htmlAttrs,
            ...baseProps
        });
    });
    if (name) {
        component.displayName = name;
    }
    return component;
};
