#!/usr/bin/env node

/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-disable no-console, stdlib/no-internal-require */

'use strict';

// MODULES //

var join = require( 'path' ).join;
var writeFileSync = require( '@stdlib/fs/write-file' ).sync;
var ls = require( '@stdlib/_tools/pkgs/names' ).sync;
var namespaceDeps = require( '@stdlib/_tools/pkgs/namespace-deps' );
var standalonePackage = require( '@stdlib/_tools/pkgs/namespace-deps/lib/standalone_package.js' ); // FIXME: avoid requiring package internals
var CompactAdjacencyMatrix = require( '@stdlib/utils/compact-adjacency-matrix' );
var indexOf = require( '@stdlib/utils/index-of' );
var contains = require( '@stdlib/assert/contains' );


// VARIABLES //

var aliases;
var results;
var pkgs;
var deps;
var mat;
var idx;
var N;
var i;
var j;


// MAIN //

console.log( 'Resolving package names...' );
pkgs = ls();
N = pkgs.length;
console.log( 'Number of packages: %d', N );

console.log( 'Converting package names to standalone package name equivalents...' );
aliases = [];
for ( i = 0; i < N; i++ ) {
	aliases.push( standalonePackage( pkgs[ i ] ) );
}

console.log( 'Generating dependency graph...' );
mat = new CompactAdjacencyMatrix( N );
for ( i = 0; i < N; i++ ) {
	// console.log( 'Processing package: %s (index: %d)', pkgs[ i ], i );
	if (
		pkgs[ i ] === '@stdlib/tools/cli' ||
		pkgs[ i ] === '@stdlib/process/cwd' ||
		pkgs[ i ] === '@stdlib/process/node-version'
	) {
		continue;
	}
	deps = namespaceDeps( pkgs[ i ], {
		'dev': false,
		'level': 999
	});
	for ( j = 0; j < deps.length; j++ ) {
		idx = indexOf( aliases, deps[ j ] );
		if ( idx >= 0 && !contains( deps[ j ], '_tools' ) ) {
			mat.addEdge( idx, i );
		}
	}
}

console.log( 'Sorting packages...' );
results = mat.toposort( pkgs );

if ( results[ 0 ] ) {
	console.log( 'Successfully sorted packages.' );
	results = results[ 0 ];
	for ( i = 0; i < results.length; i++ ) {
		results[ i ] = pkgs[ results[ i ] ];
	}
	console.log( JSON.stringify( results ) );

	// writeFileSync( join( __dirname, './ordered.json' ), JSON.stringify( results, null, '  ' ).concat( '\n' ) );
} else {
	results = results[ 1 ];
	for ( i = 0; i < results.length; i++ ) {
		results[ i ] = pkgs[ results[ i ] ];
	}
	console.error( 'Failed to sort packages. Detected the following dependency chain containing a cycle:\n\n%s', results.join( '\n' ) );
}

@@ -1949,69 +1949,20 @@
 lib/
-process/cwd' %7C%7C%0A%09%09pkgs%5B i %5D === '@stdlib/process/node-version
+fs/read-file
 '%0A%09)

Update excluded packages