@charset "UTF-8";

// @description
// * flatten function.
// * This function flattens a given list by removing nested lists.

// @access private

// @version 1.3.1

// @author Khaled Mohamed

// @license MIT

// @repository: https://github.com/Black-Axis/sass-pire

// @namespace list

// @module list/flatten

// @dependencies:
// * - meta.type-of (SASS function).
// * - list.join (SASS function).
// * - list.append (SASS function).
// * - Error.throw (function).

// @param {List} $list
// * A list that may contain nested lists to be flattened.

// @throw {Exception}
// * Throws an exception if the provided parameter is not a list.

// @example 1
// * .example1 {
// *   content: flatten((1, 2, 4), 4);
// * }

// @output 1
// * .example1 {
// *   content: 1, 2, 4, 4;
// * }

// @example 2
// * .example2 {
// *   content: flatten(((1, 2, 4), (2, ((33, 4), 67))));
// * }

// @output 2
// * .example2 {
// *   content: 1, 2, 4, 2, 33, 4, 67;
// * }

// @example 3
// * .example3 {
// *   content: flatten((1, null));
// * }

// @output 3
// * .example3 {
// *   content: 1;
// * }

// @note
// * If you have one value in the list, you can pass this one value
// * with `null` value as second value of list. It will ignore the
// * `null` value and return the another value because the SASS will read
// * the one value as not list type. You can see the example number 3 to
// * know what I mean.

// @return {List} - Returns a flattened list.

@use "sass:meta";
@use "sass:list";
@use "../../development-utils/toggle-error-message" as Error;

@function flatten($list: ()) {
    @if meta.type-of($list) != list {
        @return Error.throw("The parameter of flatten function must be in a list type.");
    }

    $flat-list: ();

    @each $item in $list {
        @if meta.type-of($item) == list {
            $flat-list: list.join($flat-list, flatten($item));
        } @else {
            $flat-list: list.append($flat-list, $item);
        }
    }

    @return $flat-list;
}
