<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}


if ( !class_exists( 'Pricely_Customer_Util' ) ) {

    class Pricely_Customer_Util {

        private static $instance;
        private $users;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            $this->users = array();
        }

        public function get_is_logged_in( $data ) {

            if ( !isset( $data[ 'wc' ][ 'customer' ][ 'id' ] ) ) {

                return false;
            }

            $user_id = $data[ 'wc' ][ 'customer' ][ 'id' ];

            return ($user_id > 0);
        }

        public function get_user_email( $data ) {

            if ( !isset( $data[ 'wc' ][ 'customer' ][ 'email' ] ) ) {

                return '';
            }

            return $data[ 'wc' ][ 'customer' ][ 'email' ];
        }

        public function get_user_roles( $data ) {

            $user = $this->get_user_by( $data );

            if ( !$user ) {

                return array();
            }

            return $user->roles;
        }

        private function get_user_by( $data ) {

            if ( !isset( $data[ 'wc' ][ 'customer' ][ 'id' ] ) ) {

                return false;
            }

            $user_id = $data[ 'wc' ][ 'customer' ][ 'id' ];

            if ( 0 >= $user_id ) {

                return false;
            }

            if ( isset( $this->users[ $user_id ] ) ) {

                return $this->users[ $user_id ];
            }

            $user = get_user_by( 'id', $user_id );

            if ( $user ) {

                $this->users[ $user_id ] = $user;
            }

            return $user;
        }

    }

}