<?php

namespace CarDealerPress\Includes\Helpers;

use CarDealerPress\Includes\Database\Requests;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Generic {
	public static function get_count_of( $data = array() ) {
		$count = 0;
		if ( ! empty( $data ) ) {
			$count = count( $data );
		}

		return $count;
	}

	public static function build_list( $args ) {
		$data   = $args['data'];
		$key    = $args['key'] ?? '';
		$list[] = $args['first_option'] ?? 'all';
		if ( $key ) {
			foreach ( $data as $element ) {
				$list[] = $element[ $key ];
			}
		} else {
			foreach ( $data as $element ) {
				$list[] = $element;
			}
		}

		return array_filter( $list );
	}

	public static function api_vehicles_params( $args = array() ) {
		$dealer_id = $args['dealer_id'];
		$page      = $args['page'] ?? 1;

		return '/' . $dealer_id . '/inventory/vehicles.json?page=' . $page . '&per_page=' . PER_PAGE_LIMIT . '&photo_view=1';
	}

	public static function select_attr_list( $attr ) {
		$results = ( new Requests() )->cdp_db_init( $attr['key'], array( 'pass' => '1' ) );

		return self::build_list(
			array(
				'data'         => $results,
				'key'          => ucfirst( $attr['key'] ),
				'first_option' => 'Select a ' . ucfirst( $attr['text'] ),
			)
		);
	}

}
