import { __ } from '@wordpress/i18n';

const TEXT_DOMAIN = 'stock-sync-with-google-sheet-for-woocommerce';

/** One grey placeholder bar / block (shimmer is CSS, off for reduced motion). */
export const Skeleton = ( { width = '100%', height = 14, radius, className = '', style } ) => (
	<span
		className={ `ssgsw-sk ${ className }` }
		aria-hidden="true"
		style={ { width, height, ...( radius !== undefined ? { borderRadius: radius } : {} ), ...style } }
	/>
);

/** Wrapper: announces "Loading" once to screen readers, hides the placeholders from them. */
export const SkeletonRegion = ( { label, className = '', children } ) => (
	<div className={ `ssgsw-sk-region ${ className }` } role="status" aria-live="polite" aria-busy="true">
		<span className="screen-reader-text">{ label || __( 'Loading…', TEXT_DOMAIN ) }</span>
		{ children }
	</div>
);

/** A row of KPI tiles. */
export const SkeletonKpis = ( { count = 6 } ) => (
	<div className="ssgsw-sk-kpis" aria-hidden="true">
		{ Array.from( { length: count } ).map( ( _, i ) => (
			<div key={ i } className="ssgsw-sk-kpi">
				<Skeleton width="55%" height={ 11 } />
				<Skeleton width="70%" height={ 24 } />
				<Skeleton width="40%" height={ 11 } />
			</div>
		) ) }
	</div>
);

/** A card with a title bar and either a chart block or text lines. */
export const SkeletonCard = ( { chart = false, lines = 4, height = 220 } ) => (
	<div className="ssgsw-sk-card" aria-hidden="true">
		<Skeleton width="30%" height={ 14 } />
		{ chart ? <Skeleton height={ height } radius={ 8 } /> : Array.from( { length: lines } ).map( ( _, i ) => <Skeleton key={ i } width={ `${ 92 - i * 9 }%` } height={ 12 } /> ) }
	</div>
);

/** A table card: toolbar line, header and rows. */
export const SkeletonTable = ( { rows = 8, cols = 6 } ) => (
	<div className="ssgsw-sk-card" aria-hidden="true">
		<div className="ssgsw-sk-row">
			{ Array.from( { length: cols } ).map( ( _, i ) => <Skeleton key={ i } height={ 11 } width="70%" /> ) }
		</div>
		{ Array.from( { length: rows } ).map( ( _, r ) => (
			<div key={ r } className="ssgsw-sk-row">
				{ Array.from( { length: cols } ).map( ( __x, i ) => <Skeleton key={ i } height={ 12 } width={ i === 1 ? '90%' : '60%' } /> ) }
			</div>
		) ) }
	</div>
);

/** Thin top progress bar shown while a report refreshes behind data already on screen. */
export const ProgressBar = ( { active } ) => (
	<div className={ `ssgsw-progress${ active ? ' is-active' : '' }` } role="presentation" aria-hidden="true"><span></span></div>
);

export default Skeleton;
