@charset "UTF-8";

// @description
// * center-of-list function.
// * A function that retrieves the middle element from a nested or flat list.

// @access private

// @version 1.4.0

// @author Khaled Mohamed

// @license MIT

// @repository: https://github.com/Black-Axis/sass-pire

// @namespace list

// @module list/center-of-list

// @dependencies:
// * - meta.type-of (SASS function).
// * - math.div (SASS function).
// * - list.length (SASS function).
// * - list.nth (SASS function).
// * - LibFunc.flatten-list (function).
// * - Error.throw (function).

// @param {List} $list
// * The input list from which the centered element is retrieved.

// @throws {Warning}
// * If the provided list is empty, the function returns 0.

// @example 1
// * .example {
// *   content: center-of-list((1, (2, 3), 4, 5));
// * }

// @output 1
// * .example {
// *   content: 3;
// * }

// @example 2
// * .example {
// *   content: center-of-list(
// *            (1, 2, 3, 4, 5, (6, 7, 12, 43, 5, (3213, 321, 65, 87, 3)
// *   )));
// * }

// @output 2
// * .example {
// *   content: 12;
// * }

// @return - The centered element.

@use "sass:meta";
@use "sass:list";
@use "sass:math";
@use "../../functions/list/flatten-list" as LibFunc;
@use "../../development-utils/toggle-error-message" as Error;

@function center-of-list($list: ()) {
    @if meta.type-of($list) != list {
        @return Error.throw("The parameter of center-of-list function must be in a list type.");
    }

    @if list.length($list) == 0 {
        @return 0;
    }

    $flatten-list: LibFunc.flatten($list);
    $flatten-list-length: list.length($flatten-list);

    @if $flatten-list-length % 2 == 0 {
        @return list.nth($flatten-list, math.div($flatten-list-length, 2));
    }

    @return list.nth($flatten-list, math.div(($flatten-list-length + 1), 2));
}
