import { __ } from '@wordpress/i18n';
import DateRangeButton from './DateRangeButton';
import { ProgressBar } from './Skeleton';

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

const RefreshIcon = () => (
	<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
		<path d="M13.5 8A5.5 5.5 0 1 1 11.9 4.1M13.5 2.5v2.8h-2.8" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
	</svg>
);

/**
 * Header of one Analytics page: title and one-line description on the left; the date range, compare and
 * refresh controls (and any page actions) on the right.
 *
 * Props: title, description, range + setRange (omit both for pages without a period), timezone,
 * showCompare, onRefresh, refreshing (a report is reloading behind data on screen), actions.
 */
export default function PageHeader( { title, description, range, setRange, timezone, showCompare = true, onRefresh, refreshing = false, actions } ) {
	return (
		<header className="ssgsw-pagehead">
			<div className="ssgsw-pagehead__text">
				<h2 className="ssgsw-pagehead__title">{ title }</h2>
				{ description && <p className="ssgsw-pagehead__desc">{ description }</p> }
			</div>
			<div className="ssgsw-pagehead__actions">
				{ actions }
				{ range && setRange && <DateRangeButton range={ range } onChange={ setRange } timezone={ timezone } showCompare={ showCompare } /> }
				{ onRefresh && (
					<button type="button" className={ `ssgsw-pagehead__refresh${ refreshing ? ' is-spinning' : '' }` } onClick={ onRefresh } disabled={ refreshing } aria-label={ __( 'Refresh', TEXT_DOMAIN ) } title={ __( 'Refresh', TEXT_DOMAIN ) }>
						<RefreshIcon />
					</button>
				) }
			</div>
			<ProgressBar active={ refreshing } />
		</header>
	);
}
