"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.flexRender = flexRender;
exports.useReactTable = useReactTable;
const React = __importStar(require("react"));
const index_1 = require("../table-core/index");
//
/**
 * If rendering headers, cells, or footers with custom markup, use flexRender instead of `cell.getValue()` or `cell.renderValue()`.
 */
function flexRender(Comp, props) {
    return !Comp ? null : isReactComponent(Comp) ? React.createElement(Comp, { ...props }) : Comp;
}
function isReactComponent(component) {
    return (isClassComponent(component) || typeof component === 'function' || isExoticComponent(component));
}
function isClassComponent(component) {
    return (typeof component === 'function' &&
        (() => {
            const proto = Object.getPrototypeOf(component);
            return proto.prototype && proto.prototype.isReactComponent;
        })());
}
function isExoticComponent(component) {
    return (typeof component === 'object' &&
        typeof component.$$typeof === 'symbol' &&
        ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description));
}
function useReactTable(options) {
    // Compose in the generic options to the user options
    const resolvedOptions = {
        state: {}, // Dummy state
        onStateChange: () => { }, // noop
        renderFallbackValue: null,
        ...options,
    };
    // Create a new table and store it in state
    const [tableRef] = React.useState(() => ({
        current: (0, index_1.createTable)(resolvedOptions),
    }));
    // By default, manage table state here using the table's initial state
    const [state, setState] = React.useState(() => tableRef.current.initialState);
    // Compose the default state above with any user state. This will allow the user
    // to only control a subset of the state if desired.
    tableRef.current.setOptions((prev) => ({
        ...prev,
        ...options,
        state: {
            ...state,
            ...options.state,
        },
        // Similarly, we'll maintain both our internal state and any user-provided
        // state.
        onStateChange: (updater) => {
            var _a;
            setState(updater);
            (_a = options.onStateChange) === null || _a === void 0 ? void 0 : _a.call(options, updater);
        },
    }));
    return tableRef.current;
}
