import queryString from 'query-string';
import { updateCategory } from '@wordpress/blocks';
import blockIcons from '@spectra-pro-helpers/block-icons';

updateCategory( 'spectra-pro-v2', {
	icon: blockIcons.logo(),
} );

/**
 * Formats parameters into a query string if they are an object; otherwise, returns them unchanged.
 *
 * @param {Object|string} params - Parameters to format; either a plain object or a string.
 * @return {string} The formatted query string if params is an object, or the original value if not.
 */
export const formatParams = ( params ) =>
	isObject( params ) ? queryString.stringify( params, { arrayFormat: 'comma' } ) : params;

/**
 * Retrieves the internal [[Class]] representation of a value as a string.
 *
 * @param {*} arg - The value to inspect.
 * @return {string} The string representation of the value's type.
 */
export const getTypeString = ( arg ) => Object.prototype.toString.call( arg );

/**
 * Determines if a value is a plain JavaScript object.
 *
 * @param {*} arg - The value to check.
 * @return {boolean} True if the value is a plain object, false otherwise.
 */
export const isObject = ( arg ) => getTypeString( arg ) === '[object Object]';

/**
 * Determines if a value is strictly null.
 *
 * @param {*} arg - The value to check.
 * @return {boolean} True if the value is null, false otherwise.
 */
export const isNull = ( arg ) => arg === null;
/**
 * Determines if a value is undefined.
 *
 * @param {*} arg - The value to check.
 * @return {boolean} True if the value is undefined, false otherwise.
 */
export const isUndefined = ( arg ) => arg === undefined;

/**
 * Determines if a value is a string.
 *
 * @param {*} arg - The value to check.
 * @return {boolean} True if the value is a string, false otherwise.
 */
export const isString = ( arg ) => typeof arg === 'string';

/**
 * Determines if a value is an array.
 *
 * @param {*} arg - The value to check.
 * @return {boolean} True if the value is an array, false otherwise.
 */
export const isArray = Array.isArray;

/**
 * Checks if a value is considered "empty" (undefined, null, empty string, empty array, or empty object).
 *
 * @param {*} arg - The value to evaluate.
 * @return {boolean} True if the value is empty, false otherwise.
 */
export const isEmpty = ( arg ) =>
	isUndefined( arg ) ||
	isNull( arg ) ||
	( isString( arg ) && ! arg.length ) ||
	( isArray( arg ) && ! arg.length ) ||
	( isObject( arg ) && ! Object.keys( arg ).length );
