/**
 * WordPress dependencies
 */
import { _x, sprintf } from '@safe-wordpress/i18n';

/**
 * External dependencies
 */
import { keyBy, filter, find, map, without, isArray, uniq } from 'lodash';
import twitter from 'twitter-text';
import type {
	AndArray,
	Maybe,
	MediaItem,
	NonEmptyArray,
	OrArray,
	RequirementsErrorAttributes,
	SocialKind,
	SocialNetwork,
	SocialNetworkName,
	SocialNetworkSupport,
	Url,
} from '@nelio-content/types';

const BASIC_AUTOMATION_FREQS: SocialNetwork[ 'automationFreqs' ] = {
	reshare: 0,
	publication: 1,
};

const GENERIC_ERRORS = {
	/* translators: %s: Social network name. */
	missingImage: _x( 'Image required by %s', 'text', 'nelio-content' ),
	/* translators: %s: Social network name. */
	missingMedia: _x(
		'Image or video required by %s',
		'text',
		'nelio-content'
	),
	/* translators: %s: Social network name. */
	missingRelatedPost: _x(
		'Add a related post on %s',
		'user',
		'nelio-content'
	),
	/* translators: %s: Social network name. */
	missingText: _x(
		'Please write status update on %s',
		'user',
		'nelio-content'
	),
	/* translators: %s: Social network name. */
	missingVideo: _x( 'Video required by %s', 'text', 'nelio-content' ),
	textTooLong: _x(
		'Please write a shorter social message',
		'user',
		'nelio-content'
	),
};

const DEFAULT_SUPPORTS: NonEmptyArray< SocialNetworkSupport > = [
	'automations',
	'image',
	'network-image',
	'network-template',
	'preview',
	'profile-template',
	'recurrence',
	'related-post',
	'reusability',
	'text',
];

const DEFAULT_REQUIRES: OrArray< AndArray< SocialNetworkSupport > > = [
	[ 'text' ],
];

const NETWORKS: NonEmptyArray< SocialNetwork > = [
	{
		id: 'twitter' as const,
		active: true,
		automationFreqs: {
			reshare: 8,
			publication: 2,
		},
		labels: {
			add: _x( 'Add X Profile', 'command', 'nelio-content' ),
			name: _x( 'X', 'text', 'nelio-content' ),
			settings: _x( 'X Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 31.039495,14.25 h 3.308 l -7.227,8.26 8.502,11.24 h -6.657 l -5.214,-6.817 -5.966,6.817 h -3.31 l 7.73,-8.835 -8.156,-10.665 h 6.826 l 4.713,6.231 z m -1.161,17.52 h 1.833 l -11.832,-15.644 h -1.967 z" />
			</svg>
		),
		limit: 280,
		supports: [
			...DEFAULT_SUPPORTS,
			'multi-image',
			'video',
			'buffer-connection',
			'hootsuite-connection',
			'link-shortener',
		],
		supportedImageTypes: [
			'image/jpeg',
			'image/jpg',
			'image/png',
			'image/gif',
			'image/webp',
		],
		supportedVideoTypes: [ 'video' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [
						'image/jpeg',
						'image/jpg',
						'image/png',
						'image/gif',
						'image/webp',
					].includes( media.mime )
				) {
					return _x(
						'Unsupported image type in X (Twitter)',
						'text',
						'nelio-content'
					);
				}

				if (
					media.mime !== 'image/gif' &&
					megas( media.filesizeInBytes ) > 5
				) {
					return _x(
						'X (Twitter) image should be smaller than 5MB',
						'text',
						'nelio-content'
					);
				}

				if (
					media.mime === 'image/gif' &&
					megas( media.filesizeInBytes ) > 15
				) {
					return _x(
						'X (Twitter) GIF image should be smaller than 15MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( media.duration < 0.5 ) {
					return _x(
						'X (Twitter) video must be at least half a second long',
						'text',
						'nelio-content'
					);
				}

				if ( media.duration > 140 ) {
					return _x(
						'X (Twitter) video must be shorter than 140 seconds',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 512 ) {
					return _x(
						'X (Twitter) video should be smaller than 512MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'facebook' as const,
		active: true,
		automationFreqs: {
			reshare: 1,
			publication: 1,
		},
		labels: {
			add: _x( 'Add Facebook Profile', 'command', 'nelio-content' ),
			name: _x( 'Facebook', 'text', 'nelio-content' ),
			settings: _x( 'Facebook Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 27.025,13.95 h 4.95 v -6 h -4.95 c -3.9,0 -7.05,3.15 -7.05,7.05 v 3 h -4.05 v 6 h 4.05 v 16.05 h 6 V 24 h 4.95 l 1.05,-6 h -6 v -3 c 0,-0.6 0.45,-1.05 1.05,-1.05" />
			</svg>
		),
		limit: 10000,
		supports: [
			...DEFAULT_SUPPORTS,
			'buffer-connection',
			'hootsuite-connection',
			'video',
			'link-shortener',
		],
		supportedImageTypes: [
			'image/jpeg',
			'image/jpg',
			'image/png',
			'image/gif',
			'image/tiff',
		],
		supportedVideoTypes: [ 'video/mp4' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( megas( media.filesizeInBytes ) > 4 ) {
					return _x(
						'Facebook image should be smaller than 4MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( 'video/mp4' !== media.mime ) {
					return _x(
						'Unsupported video type in Facebook',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
		kinds: [
			{
				id: 'page',
				label: _x( 'Page', 'facebook profile', 'nelio-content' ),
			},
		],
	},

	{
		id: 'linkedin' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add LinkedIn Profile', 'command', 'nelio-content' ),
			name: _x( 'LinkedIn', 'text', 'nelio-content' ),
			settings: _x( 'LinkedIn Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 13.05,10.975 c -1.65,0 -3,1.35 -3,3 0,1.65 1.35,3 3,3 1.65,0 3,-1.35 3,-3 0,-1.65 -1.35,-3 -3,-3 m -3,7.95 v 18 h 6 v -18 h -6 m 10.05,0 v 18 h 5.7 v -8.7 c 0,-2.25 0.45,-4.5 3.45,-4.5 3,0 3,2.55 3,4.65 v 8.55 h 5.7 v -9.6 c 0,-4.8 -1.05,-8.4 -6.9,-8.4 -2.85,0 -4.65,1.5 -5.4,2.85 H 25.5 v -2.85 h -5.55" />
			</svg>
		),
		limit: 3000,
		supports: [
			...DEFAULT_SUPPORTS,
			'buffer-connection',
			'hootsuite-connection',
			'multi-image',
			'video',
			'link-shortener',
		],
		supportedImageTypes: [
			'image/jpeg',
			'image/jpg',
			'image/png',
			'image/gif',
		],
		supportedVideoTypes: [ 'video/mp4' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [
						'image/jpeg',
						'image/jpg',
						'image/png',
						'image/gif',
						'image/tiff',
					].includes( media.mime )
				) {
					return _x(
						'Unsupported image type in LinkedIn',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( media.mime !== 'video/mp4' ) {
					return _x(
						'Unsupported video type in LinkedIn',
						'text',
						'nelio-content'
					);
				}

				if ( media.duration < 3 ) {
					return _x(
						'LinkedIn video must be at least 3 seconds long',
						'text',
						'nelio-content'
					);
				}

				if ( media.duration > 30 * 60 ) {
					return _x(
						'LinkedIn video must be shorter than 30 minutes',
						'text',
						'nelio-content'
					);
				}

				if ( kilos( media.filesizeInBytes ) < 75 ) {
					return _x(
						'LinkedIn video must be at least 75KB',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 500 ) {
					return _x(
						'LinkedIn video should be smaller than 500MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
		kinds: [
			{
				id: 'single',
				label: _x( 'Personal', 'linkedin profile', 'nelio-content' ),
			},
			{
				id: 'company',
				label: _x( 'Company', 'linkedin profile', 'nelio-content' ),
			},
		],
	},

	{
		id: 'gmb' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x(
				'Add Google My Business Location',
				'command',
				'nelio-content'
			),
			name: _x( 'Google My Business', 'text', 'nelio-content' ),
			settings: _x(
				'Google My Business Settings',
				'text',
				'nelio-content'
			),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 24.27499,11.7 c -6.75,0 -12.3,5.55 -12.3,12.3 0,6.75 5.55,12.3 12.3,12.3 7.05006,0 11.85006,-4.95 11.85006,-12 0,-0.75 -0.1501,-1.5 -0.1501,-2.1 H 24.27499 v 4.2 h 6.90006 c -0.3,1.8 -2.1,5.25 -6.90006,5.25 -4.2,0 -7.65,-3.45 -7.65,-7.8 0,-4.35 3.45,-7.8 7.65,-7.8 2.4,0 3.89996,1.05 4.94996,1.95 l 3.3001,-3.15 c -2.1,-1.95 -4.9501,-3.15 -8.25006,-3.15" />
			</svg>
		),
		limit: 1500,
		supports: [
			...DEFAULT_SUPPORTS,
			'buffer-connection',
			'link-shortener',
		],
		supportedImageTypes: [ 'image/jpeg', 'image/jpg', 'image/png' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [ 'image/jpeg', 'image/jpg', 'image/png' ].includes(
						media.mime
					)
				) {
					return _x(
						'Unsupported image type in Google My Business',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 5 ) {
					return _x(
						'Google My Business image should be smaller than 5MB',
						'text',
						'nelio-content'
					);
				}

				if ( kilos( media.filesizeInBytes ) < 10 ) {
					return _x(
						'Google My Business image should be at least 10KB',
						'text',
						'nelio-content'
					);
				}

				if ( media.height < 250 ) {
					return _x(
						'Google My Business image height must be at least 250px',
						'text',
						'nelio-content'
					);
				}

				if ( media.width < 250 ) {
					return _x(
						'Google My Business image width must be at least 250px',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
		kinds: [
			{
				id: 'location',
				label: _x(
					'Location',
					'Google My Business profile',
					'nelio-content'
				),
			},
		],
	},

	{
		id: 'pinterest' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Pinterest Profile', 'command', 'nelio-content' ),
			name: _x( 'Pinterest', 'text', 'nelio-content' ),
			settings: _x( 'Pinterest Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 12.15,19.975 c 0,-1.35 0.3,-2.55 0.75,-3.75 0.45,-1.2 1.05,-2.1 1.8,-3 0.75,-0.9 1.65,-1.65 2.7,-2.25 1.05,-0.6 2.1,-1.05 3.3,-1.35 1.2,-0.3 2.4,-0.45 3.6,-0.45 1.95,0 3.75,0.45 5.4,1.2 1.65,0.75 3,1.95 4.05,3.45 1.05,1.5 1.5,3.3 1.5,5.25 0,1.2 -0.15,2.25 -0.3,3.45 -0.3,1.05 -0.6,2.25 -1.05,3.15 -0.45,1.05 -1.05,1.95 -1.8,2.7 -0.75,0.75 -1.65,1.35 -2.7,1.8 -1.05,0.45 -2.25,0.75 -3.45,0.75 -0.75,0 -1.65,-0.15 -2.4,-0.6 -0.75,-0.45 -1.35,-0.9 -1.8,-1.65 -0.15,0.45 -0.3,1.2 -0.45,2.1 -0.15,0.9 -0.3,1.5 -0.45,1.65 0,0.3 -0.15,0.75 -0.3,1.35 -0.15,0.6 -0.3,1.05 -0.45,1.35 l -0.6,1.2 -0.9,1.35 c -0.3,0.45 -0.75,0.9 -1.05,1.5 l -0.3,0.15 -0.15,-0.15 c -0.15,-1.95 -0.3,-3 -0.3,-3.45 0,-1.05 0.15,-2.4 0.45,-3.75 0.3,-1.35 0.6,-3.15 1.2,-5.25 0.6,-2.1 0.9,-3.3 0.9,-3.75 -0.45,-0.75 -0.6,-1.8 -0.6,-3 0,-1.05 0.3,-1.95 0.9,-2.85 0.6,-0.9 1.5,-1.35 2.4,-1.35 0.75,0 1.35,0.3 1.65,0.75 0.45,0.45 0.6,1.05 0.6,1.8 0,0.75 -0.3,1.95 -0.75,3.45 -0.6,1.5 -0.75,2.7 -0.75,3.45 0,0.75 0.3,1.35 0.75,1.95 0.6,0.45 1.2,0.75 1.95,0.75 0.6,0 1.35,-0.15 1.8,-0.45 0.6,-0.3 1.05,-0.75 1.35,-1.2 0.45,-0.45 0.75,-1.05 1.05,-1.65 0.3,-0.6 0.6,-1.35 0.75,-1.95 0.15,-0.75 0.3,-1.35 0.3,-1.95 0.15,-0.6 0.15,-1.2 0.15,-1.8 0,-2.1 -0.6,-3.75 -1.95,-4.95 -1.35,-1.2 -3,-1.8 -5.1,-1.8 -2.4,0 -4.5,0.75 -6,2.4 -1.65,1.5 -2.4,3.6 -2.4,6 0,0.6 0.15,1.05 0.3,1.5 0.15,0.45 0.3,0.9 0.45,1.2 0.15,0.3 0.3,0.6 0.45,0.75 0.15,0.3 0.3,0.45 0.3,0.6 0,0.3 -0.15,0.75 -0.3,1.35 -0.15,0.6 -0.45,0.75 -0.6,0.75 h -0.3 c -0.6,-0.15 -1.2,-0.45 -1.65,-1.05 -0.45,-0.45 -0.9,-1.05 -1.05,-1.65 -0.3,-0.6 -0.45,-1.35 -0.6,-1.95 -0.15,-0.6 -0.15,-1.35 -0.15,-1.95" />
			</svg>
		),
		limit: 500,
		supports: [
			...without( DEFAULT_SUPPORTS, 'network-template' ),
			'multi-image',
			'multi-target',
			'buffer-connection',
			'link-shortener',
		],
		supportedImageTypes: [ 'image' ],
		requires: [ [ 'multi-target', 'text', 'image' ] ],
		validators: {
			images: ( media ) => {
				if (
					uniq( media.map( ( m ) => `${ m.width }-${ m.height }` ) )
						.length !== 1
				) {
					return _x(
						'Images in a multi-image Pinterest pin must have the same width/height ratios.',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'reddit' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Subreddit', 'command', 'nelio-content' ),
			name: _x( 'Reddit', 'text', 'nelio-content' ),
			settings: _x( 'Reddit Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 34.8133,8.60832 c -0.3054,-0.001 -0.6126,0.0486 -0.9129,0.15272 -0.5974,0.2073 -1.1094,0.62592 -1.4242,1.16445 -0.043,0.0748 -0.086,0.1449 -0.091,0.15544 -0.015,0.015 -0.3845,-0.0598 -1.7294,-0.34492 -5.0433,-1.06905 -4.5751,-0.97337 -4.7326,-0.96729 -0.1245,0.005 -0.1541,0.012 -0.242,0.0548 -0.123,0.0603 -0.2389,0.17388 -0.2935,0.28587 -0.031,0.0663 -0.236,1.00464 -1.0032,4.61985 -0.5295,2.49525 -0.9659,4.5504 -0.9698,4.56705 0,0.029 -0.024,0.031 -0.3925,0.0486 -2.8154,0.1344 -5.5287,0.81712 -7.8311,1.97025 -0.6039,0.30246 -1.1124,0.59826 -1.6728,0.973 -0.1999,0.1338 -0.3664,0.24321 -0.3699,0.24321 0,0 -0.078,-0.0606 -0.1645,-0.13485 -0.5259,-0.4477 -1.1961,-0.75444 -1.8911,-0.86545 -0.2031,-0.0324 -0.7128,-0.0447 -0.9343,-0.0226 -1.0103,0.1011 -1.9554,0.61365 -2.5824,1.40047 -0.225,0.28244 -0.4785,0.73029 -0.5909,1.04403 -0.2641,0.73724 -0.306,1.5018 -0.1215,2.2296 0.2679,1.06035 0.9525,1.929 1.9224,2.43915 l 0.1757,0.0926 -0.018,0.12585 c -0.085,0.58338 -0.09,1.34156 -0.015,1.9548 0.093,0.72237 0.3039,1.47101 0.6129,2.1696 1.2492,2.8245 4.191,5.1807 8.0422,6.4413 1.7778,0.5819 3.6788,0.91089 5.694,0.98541 0.3981,0.015 1.3346,0.008 1.7697,-0.015 4.0667,-0.20247 7.7847,-1.5021 10.4256,-3.64425 1.073,-0.87034 1.9553,-1.87995 2.5614,-2.931 0.2595,-0.45013 0.5396,-1.06833 0.695,-1.5345 0.3565,-1.06959 0.4783,-2.23995 0.3421,-3.2859 -0.017,-0.1221 -0.029,-0.23352 -0.029,-0.24744 -1e-4,-0.0174 0.051,-0.0516 0.1644,-0.1095 0.188,-0.0961 0.4724,-0.282 0.644,-0.42078 0.1501,-0.1215 0.432,-0.40335 0.5535,-0.55347 0.4275,-0.52848 0.7117,-1.17679 0.8166,-1.86225 0.043,-0.28059 0.046,-0.78865 0.015,-1.0567 -0.2151,-1.48095 -1.2101,-2.66385 -2.6142,-3.10815 -1.0358,-0.32769 -2.1933,-0.18375 -3.1173,0.38763 -0.15,0.0927 -0.4293,0.30205 -0.5616,0.4207 -0.063,0.0561 -0.091,0.0734 -0.1065,0.063 -0.1544,-0.11055 -0.7271,-0.48358 -0.9263,-0.60336 -0.7329,-0.44083 -1.5366,-0.84036 -2.362,-1.17424 -1.9941,-0.80661 -4.317,-1.30416 -6.5715,-1.40759 -0.12,-0.006 -0.2184,-0.0135 -0.2184,-0.0192 0,-0.005 0.3865,-1.8174 0.8589,-4.02735 0.4725,-2.20995 0.8589,-4.02405 0.8589,-4.0314 0,-0.008 0.015,-0.0135 0.025,-0.0135 0.015,9e-5 1.2786,0.26868 2.8096,0.59685 l 2.7837,0.59664 0.019,0.17696 c 0.1335,1.22904 1.0703,2.2017 2.2973,2.38395 0.2001,0.0297 0.583,0.0299 0.7755,3e-4 0.7893,-0.1212 1.4814,-0.57411 1.8951,-1.2401 0.3138,-0.50526 0.4585,-1.1253 0.3961,-1.6971 C 37.374,10.0916 36.8069,9.28041 35.9679,8.87811 35.5989,8.70119 35.2092,8.6108 34.8167,8.60795 Z m -16.8105,15.6465 c 0.1667,-7.6e-4 0.3395,0.0105 0.4643,0.0312 0.5662,0.0969 1.0416,0.34062 1.4454,0.74087 0.334,0.33109 0.5541,0.69388 0.6861,1.13095 0.071,0.23633 0.099,0.41298 0.108,0.68495 0.015,0.44938 -0.066,0.84906 -0.2531,1.24158 -0.2358,0.49696 -0.6546,0.93786 -1.1506,1.21143 -0.23,0.1269 -0.5582,0.23785 -0.8433,0.28516 -0.1706,0.0283 -0.5585,0.04 -0.7109,0.0214 -0.3237,-0.0394 -0.5916,-0.1167 -0.8706,-0.25126 -0.6933,-0.33429 -1.1956,-0.91884 -1.4157,-1.6473 -0.07,-0.23321 -0.099,-0.41135 -0.108,-0.67838 -0.016,-0.46395 0.064,-0.84439 0.2679,-1.2655 0.39,-0.8048 1.1328,-1.34774 2.0282,-1.4825 0.098,-0.015 0.223,-0.0221 0.3526,-0.0226 z m 11.897,0.0396 c 0.4044,-6e-4 0.5325,0.0181 0.8551,0.1257 0.8549,0.28454 1.5161,0.98825 1.7424,1.85415 0.072,0.27602 0.082,0.36627 0.082,0.69267 -6e-4,0.34244 -0.021,0.48749 -0.105,0.76658 -0.129,0.42846 -0.3319,0.77059 -0.6493,1.09723 -0.4472,0.46005 -1.0035,0.73184 -1.6401,0.80102 -0.135,0.015 -0.4077,0.0178 -0.5079,0.006 -0.5604,-0.0666 -0.9825,-0.22875 -1.3829,-0.53121 -0.5917,-0.44709 -0.9667,-1.10478 -1.0545,-1.84965 -0.023,-0.18549 -0.015,-0.57791 0.024,-0.76377 0.1734,-0.96498 0.8637,-1.7628 1.7934,-2.07225 0.3272,-0.1089 0.4395,-0.12585 0.8426,-0.1263 z m -11.774,8.5971 c 0.064,5.3e-4 0.1065,0.007 0.1664,0.0265 0.1185,0.0365 0.1894,0.0838 0.3804,0.25604 0.2808,0.25344 0.5287,0.41854 0.9274,0.61758 1.6211,0.80922 4.2038,1.08934 6.4938,0.70426 1.4712,-0.24738 2.5685,-0.69828 3.2541,-1.33732 0.1673,-0.15587 0.2271,-0.19628 0.3545,-0.23931 0.077,-0.0258 0.12,-0.0306 0.2344,-0.0259 0.12,0.005 0.1551,0.012 0.2421,0.0532 0.1365,0.0639 0.2103,0.1254 0.279,0.23161 0.09,0.1392 0.1215,0.26154 0.1125,0.44694 0,0.144 -0.015,0.162 -0.068,0.2772 -0.05,0.10035 -0.085,0.1464 -0.1912,0.24878 -0.5387,0.51915 -1.3194,0.96475 -2.2431,1.28017 -0.9609,0.3281 -2.1078,0.53298 -3.3516,0.5987 -0.2501,0.0135 -1.1229,0.0168 -1.3181,0.006 -0.8583,-0.0501 -1.4652,-0.12345 -2.125,-0.25682 -1.5095,-0.30501 -2.6273,-0.79269 -3.4533,-1.5066 -0.2825,-0.24405 -0.3762,-0.37638 -0.4131,-0.58285 -0.053,-0.29208 0.1095,-0.59255 0.3939,-0.73322 0.102,-0.0507 0.129,-0.0571 0.2524,-0.0625 0.027,-10e-4 0.051,-10e-4 0.072,-10e-4 z" />
			</svg>
		),
		limit: 500,
		supports: without( DEFAULT_SUPPORTS, 'network-image' ),
		supportedImageTypes: [ 'image' ],
		requires: DEFAULT_REQUIRES,
		kinds: [
			{
				id: 'subreddit',
				label: _x( 'Subreddit', 'Reddit profile', 'nelio-content' ),
			},
		],
	},

	{
		id: 'instagram' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Instagram Profile', 'command', 'nelio-content' ),
			name: _x( 'Instagram', 'text', 'nelio-content' ),
			settings: _x( 'Instagram Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 18.9501,24.05 c 0,-2.85 2.25,-5.1 5.0998,-5.1 2.8502,0 5.1002,2.25 5.1002,5.1 0,2.85 -2.25,5.1 -5.1002,5.1 -2.8498,0 -5.0998,-2.25 -5.0998,-5.1 m -2.85,0 c 0,4.35 3.6,7.95 7.9498,7.95 4.3502,0 7.9502,-3.6 7.9502,-7.95 0,-4.35 -3.6,-7.95 -7.9502,-7.95 -4.3498,0 -7.9498,3.6 -7.9498,7.95 m 14.25,-8.25 c 0,1.05 0.9,1.8 1.8,1.8 1.05,0 1.8,-0.9 1.8,-1.8 0,-1.05 -0.9,-1.8 -1.8,-1.8 -1.05,0 -1.8,0.9 -1.8,1.8 m -12.6,20.7 c -1.5,0 -2.25,-0.3 -2.85,-0.6 -0.75,-0.3 -1.2,-0.6 -1.8,-1.2 -0.6,-0.6 -0.9,-1.05 -1.2,-1.8 -0.15,-0.6 -0.45,-1.35 -0.6,-2.85 0,-1.65 -0.15,-2.1 -0.15,-6.3 0,-4.05 0,-4.65 0.15,-6.3 0,-1.5 0.3,-2.25 0.6,-2.85 0.3,-0.75 0.6,-1.2 1.2,-1.8 0.6,-0.6 1.05,-0.9 1.8,-1.2 0.6,-0.15 1.35,-0.45 2.85,-0.6 1.65,0 2.1,-0.15 6.3,-0.15 4.05,0 4.65,0 6.3,0.15 1.5,0 2.25,0.3 2.85,0.6 0.75,0.3 1.2,0.6 1.8,1.2 0.6,0.6 0.9,1.05 1.2,1.8 0.15,0.6 0.45,1.35 0.6,2.85 0.15,1.65 0.15,2.1 0.15,6.3 0,4.05 0,4.65 -0.15,6.3 0,1.5 -0.3,2.25 -0.6,2.85 -0.3,0.75 -0.6,1.2 -1.2,1.8 -0.6,0.6 -1.05,0.9 -1.8,1.2 -0.6,0.15 -1.35,0.45 -2.85,0.6 -1.65,0 -2.1,0.15 -6.3,0.15 -4.05,0 -4.65,0 -6.3,-0.15 m -0.15,-27.9 c -1.65,0 -2.7,0.3 -3.75,0.75 -1.0502,0.45 -1.8002,0.9 -2.7,1.8 -0.9,0.9 -1.35,1.65 -1.8,2.7 -0.45,1.05 -0.6,2.1 -0.75,3.75 -0.15,1.65 -0.15,2.1 -0.15,6.3 0,4.2 0,4.65 0.15,6.3 0.15,1.65 0.3,2.7 0.75,3.75 0.4498,1.05 0.9,1.8 1.8,2.7 0.9,0.9 1.65,1.35 2.7,1.8 1.05,0.45 2.1,0.6 3.75,0.75 1.65,0 2.1,0.15 6.3,0.15 4.2,0 4.65,0 6.3,-0.15 1.65,0 2.7,-0.3 3.75,-0.75 1.05,-0.45 1.8,-0.9 2.7,-1.8 0.9,-0.9 1.35,-1.65 1.8,-2.7 0.45,-1.05 0.6,-2.1 0.75,-3.75 0.15,-1.65 0.15,-2.1 0.15,-6.3 0,-4.2 0,-4.65 -0.15,-6.3 0,-1.65 -0.3,-2.7 -0.75,-3.75 -0.45,-1.05 -0.9,-1.8 -1.8,-2.7 -0.9,-0.9 -1.65,-1.35 -2.7,-1.8 -1.05,-0.45 -2.1,-0.6 -3.75,-0.75 -1.65,0 -2.1,-0.15 -6.3,-0.15 -4.2,0 -4.65,0 -6.3,0.15" />
			</svg>
		),
		limit: 2000,
		supports: [
			...DEFAULT_SUPPORTS,
			'buffer-connection',
			'hootsuite-connection',
			'multi-image',
			'multi-media',
			'video',
			'link-shortener',
		],
		supportedImageTypes: [ 'image/jpeg', 'image/jpg' ],
		supportedVideoTypes: [ 'video/mp4', 'video/quicktime' ],
		requires: [ [ 'text', 'image' ] ],
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( ! [ 'image/jpeg', 'image/jpg' ].includes( media.mime ) ) {
					return _x(
						'Unsupported image type in Instagram',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 8 ) {
					return _x(
						'Instagram image should be smaller than 8MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [ 'video/mp4', 'video/quicktime' ].includes( media.mime )
				) {
					return _x(
						'Unsupported video type in Instagram',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'telegram' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Telegram Connection', 'command', 'nelio-content' ),
			name: _x( 'Telegram', 'text', 'nelio-content' ),
			settings: _x( 'Telegram Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 9.1562,23.47507 c 8.4443,-3.66723 14.0658,-6.10401 16.8885,-7.2862 8.0342,-3.35358 9.723,-3.93261 10.8087,-3.95675 0.2412,0 0.7719,0.0483 1.134,0.33777 0.2894,0.24128 0.3618,0.55491 0.4101,0.79619 0.048,0.24126 0.097,0.7479 0.048,1.13394 -0.4344,4.58403 -2.3161,15.70633 -3.2812,20.82114 -0.4103,2.17138 -1.2063,2.89519 -1.9784,2.96757 -1.6888,0.14475 -2.9676,-1.10982 -4.584,-2.17139 -2.5575,-1.66473 -3.9808,-2.70216 -6.4659,-4.34277 -2.8711,-1.88185 -1.0134,-2.9193 0.6273,-4.60816 0.434,-0.43427 7.841,-7.1897 7.9859,-7.79285 0.024,-0.0725 0.024,-0.36189 -0.1455,-0.50665 -0.1686,-0.14475 -0.4101,-0.0965 -0.603,-0.0483 -0.2654,0.0483 -4.3187,2.75041 -12.2081,8.08237 -1.158,0.79617 -2.1954,1.1822 -3.1363,1.15806 -1.0376,-0.0241 -3.0158,-0.57903 -4.5117,-1.06155 C 8.3351,26.41845 6.8876,26.1048 7.0081,25.0915 7.0801,24.56071 7.8043,24.02994 9.1555,23.47503 Z" />
			</svg>
		),
		limit: 4096,
		supports: [
			...DEFAULT_SUPPORTS,
			'multi-image',
			'video',
			'multi-media',
			'link-shortener',
		],
		supportedImageTypes: [ 'image' ],
		supportedVideoTypes: [ 'video' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( megas( media.filesizeInBytes ) > 5 ) {
					return _x(
						'Telegram image should be smaller than 5MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( megas( media.filesizeInBytes ) > 20 ) {
					return _x(
						'Telegram video should be smaller than 20MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'medium' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Medium Connection', 'command', 'nelio-content' ),
			name: _x( 'Medium', 'text', 'nelio-content' ),
			settings: _x( 'Medium Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="M73.585-6.318c0 5.368-4.322 9.72-9.653 9.72-5.332 0-9.654-4.352-9.654-9.72s4.322-9.72 9.654-9.72c5.331 0 9.653 4.352 9.653 9.72M84.176-6.318c0 5.053-2.161 9.151-4.827 9.151s-4.827-4.098-4.827-9.15c0-5.054 2.16-9.152 4.826-9.152 2.666 0 4.827 4.097 4.827 9.151M88.507-6.318c0 4.526-.76 8.198-1.697 8.198-.938 0-1.698-3.67-1.698-8.198 0-4.527.76-8.198 1.698-8.198.937 0 1.697 3.67 1.697 8.198" />
			</svg>
		),
		limit: 4096,
		tagsLimit: 5,
		supports: [ 'preview', 'related-post', 'medium-tags' ],
		requires: [ [ 'preview', 'related-post' ] ],
		kinds: [
			{
				id: 'single',
				label: _x( 'Personal', 'medium profile', 'nelio-content' ),
			},
			{
				id: 'publication',
				label: _x( 'Publication', 'medium profile', 'nelio-content' ),
			},
		],
	},

	{
		id: 'blogger' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Blogger Connection', 'command', 'nelio-content' ),
			name: _x( 'Blogger', 'text', 'nelio-content' ),
			settings: _x( 'Blogger Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="M19.822 9.729H18.73a1.113 1.113 0 0 1-1.093-1.094A5.636 5.636 0 0 0 12 3H8.636A5.636 5.636 0 0 0 3 8.635v6.73A5.636 5.636 0 0 0 8.636 21h6.729A5.636 5.636 0 0 0 21 15.364v-4.458a1.15 1.15 0 0 0-1.178-1.177M8.552 7.542h4.037c.589 0 1.093.504 1.093 1.093S13.178 9.73 12.59 9.73H8.55a1.113 1.113 0 0 1-1.093-1.094c0-.589.505-1.093 1.093-1.093m6.897 8.916H8.636a1.113 1.113 0 0 1-1.094-1.094c0-.589.505-1.093 1.094-1.093h6.813c.588 0 1.093.504 1.093 1.093s-.505 1.094-1.093 1.094" />
			</svg>
		),
		limit: 10000,
		supports: [ 'preview', 'related-post' ],
		requires: [ [ 'preview', 'related-post' ] ],
		kinds: [
			{
				id: 'blog',
				label: _x( 'Blog', 'Blogger blog', 'nelio-content' ),
			},
		],
	},

	{
		id: 'tiktok' as const,
		active: true,
		automationFreqs: {
			reshare: 0,
			publication: 0,
		},
		labels: {
			add: _x( 'Add TikTok Profile', 'command', 'nelio-content' ),
			name: _x( 'TikTok', 'text', 'nelio-content' ),
			settings: _x( 'TikTok Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="M 37.525,21.21877 V 15.99544 C 33.3235,15.61359 30.3877,12.8152 30.151,8.49843 h -5.3259 v 21.50782 c 0.072,0.43079 -0.2088,4.06956 -4.7982,4.18668 -2.3523,0.0602 -4.2642,-1.91922 -4.2859,-4.58934 -0.021,-2.55037 2.4891,-5.0956 5.8888,-4.26762 v -5.32573 c -5.0398,-0.94718 -11.088,3.32887 -11.154,8.98603 -0.057,4.89774 2.808,10.5053 9.6164,10.5053 7.0797,0 10.0588,-5.88327 10.0588,-9.24953 V 18.90412 c 1.3508,1.30703 5.4431,2.34515 7.374,2.31465 z" />
			</svg>
		),
		limit: 2000,
		supports: [
			'recurrence',
			'reusability',
			'text',
			'preview',
			'video',
			'image',
			'multi-image',
			'link-shortener',
		],
		supportedImageTypes: [ 'image/jpeg', 'image/jpg', 'image/webp' ],
		supportedVideoTypes: [ 'video/mp4', 'video/webm', 'video/quicktime' ],
		requires: [ [ 'video' ], [ 'image' ], [ 'multi-image' ] ],
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [ 'image/jpeg', 'image/jpg', 'image/webp' ].includes(
						media.mime
					)
				) {
					return _x(
						'Unsupported image type in TikTok',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 20 ) {
					return _x(
						'TikTok image should be smaller than 20MB',
						'text',
						'nelio-content'
					);
				}

				if ( media.height > 1080 ) {
					return _x(
						'TikTok image height must be less than or equal to 1080px',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [ 'video/mp4', 'video/webm', 'video/quicktime' ].includes(
						media.mime
					)
				) {
					return _x(
						'Unsupported video type in TikTok',
						'text',
						'nelio-content'
					);
				}

				if ( media.duration > 10 * 60 ) {
					return _x(
						'TikTok video must be shorter than 10 minutes',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			general: ( attrs ) => {
				if (
					attrs.network === 'tiktok' &&
					attrs.tiktokSettings?.discloseContent &&
					! attrs.tiktokSettings.isBrandContent &&
					! attrs.tiktokSettings.isBrandOrganic
				) {
					return _x(
						'You need to indicate if your content promotes yourself, a third party, or both',
						'user',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'tumblr' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Tumblr Profile', 'command', 'nelio-content' ),
			name: _x( 'Tumblr', 'text', 'nelio-content' ),
			settings: _x( 'Tumblr Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 21.175,8.58184 c 0,6.45 -5.25,8.7 -5.25,8.7 v 4.05 h 3.3 v 12.9 c 0,6 7.05,6 12.75,4.05 v -4.95 c -4.95,2.85 -7.5,1.05 -7.5,-2.55 v -9.45 h 6.75 v -4.8 h -6.75 v -8.1 h -3.3" />
			</svg>
		),
		limit: 500,
		supports: DEFAULT_SUPPORTS,
		supportedImageTypes: [ 'image' ],
		requires: DEFAULT_REQUIRES,
	},

	{
		id: 'mastodon' as const,
		active: true,
		automationFreqs: {
			reshare: 8,
			publication: 2,
		},
		labels: {
			add: _x( 'Add Mastodon Profile', 'command', 'nelio-content' ),
			name: _x( 'Mastodon', 'text', 'nelio-content' ),
			settings: _x( 'Mastodon Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m 23.952,7.43706 c -4.2348,0.0346 -8.3107,0.49252 -10.6846,1.5828 0,0 -4.7073,2.10789 -4.7073,9.29386 0,1.6455 -0.033,3.61106 0.02,5.69757 0.1711,7.0275 1.2903,13.95473 7.788,15.67448 2.9957,0.79296 5.5654,0.95875 7.6371,0.84489 3.7566,-0.20826 5.8676,-1.34141 5.8676,-1.34141 l -0.123,-2.72397 c 0,0 -2.6855,0.84656 -5.7005,0.7434 -2.9868,-0.10245 -6.1416,-0.32389 -6.6248,-3.9913 -0.045,-0.32216 -0.066,-0.66693 -0.066,-1.02869 0,0 2.9306,0.71852 6.6468,0.88878 2.2723,0.10425 4.4058,-0.13395 6.5699,-0.39228 4.1504,-0.4956 7.7644,-3.05359 8.2186,-5.39032 0.7157,-3.68099 0.6555,-8.98115 0.6555,-8.98115 0,-7.18597 -4.7073,-9.29386 -4.7073,-9.29386 C 32.3683,7.92958 28.2926,7.47168 24.0575,7.43706 Z m -4.795,5.61528 c 1.7641,0 3.0998,0.67647 3.9831,2.03268 l 0.8586,1.44016 0.8586,-1.44016 c 0.8831,-1.35621 2.2215,-2.03268 3.9858,-2.03268 1.5245,0 2.7516,0.53455 3.6896,1.58005 0.9093,1.04549 1.3606,2.45991 1.3606,4.23819 v 8.70135 H 30.4478 V 19.1257 c 0,-1.78026 -0.7485,-2.68281 -2.2467,-2.68281 -1.6566,0 -2.488,1.07078 -2.488,3.19029 v 4.62225 h -3.4263 v -4.62225 c 0,-2.11951 -0.8313,-3.19029 -2.4879,-3.19029 -1.4982,0 -2.2469,0.90255 -2.2469,2.68281 v 8.44623 h -3.4482 v -8.70135 c 0,-1.77828 0.4542,-3.1927 1.3634,-4.23819 0.9379,-1.0455 2.1651,-1.58005 3.6898,-1.58005 z" />
			</svg>
		),
		limit: 500,
		supports: [
			...DEFAULT_SUPPORTS,
			'multi-image',
			'video',
			'buffer-connection',
			'link-shortener',
		],
		supportedImageTypes: [ 'image' ],
		supportedVideoTypes: [
			'video/mp4',
			'video/x-m4v',
			'video/quicktime',
			'video/webm',
		],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( megas( media.filesizeInBytes ) > 16 ) {
					return _x(
						'Mastodon image should be smaller than 16MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [
						'video/mp4',
						'video/x-m4v',
						'video/quicktime',
						'video/webm',
					].includes( media.mime )
				) {
					return _x(
						'Unsupported video type in Mastodon',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 99 ) {
					return _x(
						'Mastodon video should be smaller than 99MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'bluesky' as const,
		active: true,
		automationFreqs: {
			reshare: 8,
			publication: 2,
		},
		labels: {
			add: _x( 'Add Bluesky Profile', 'command', 'nelio-content' ),
			name: _x( 'Bluesky', 'text', 'nelio-content' ),
			settings: _x( 'Bluesky Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="M24 21.6c-2.16 -4.2 -8.1 -12.12 -13.59 -15.99C5.1 1.89 3.12 2.52 1.8 3.12 0.3 3.81 0 6.15 0 7.53s0.75 11.31 1.26 12.96c1.62 5.46 7.41 7.35 12.75 6.75 -7.8 1.14 -14.76 3.99 -5.64 14.13 9.99 10.38 13.71 -2.22 15.63 -8.61 1.92 6.39 4.11 18.54 15.48 8.61 8.52 -8.61 2.34 -12.99 -5.49 -14.13 5.34 0.6 11.13 -1.29 12.75 -6.75 0.51 -1.65 1.26 -11.58 1.26 -12.96s-0.27 -3.72 -1.8 -4.41c-1.32 -0.6 -3.33 -1.26 -8.61 2.49A53.7 53.7 0 0 0 24 21.6" />
			</svg>
		),
		limit: 300,
		supports: [ ...DEFAULT_SUPPORTS, 'multi-image', 'link-shortener' ],
		supportedImageTypes: [ 'image' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( megas( media.filesizeInBytes ) > 1 ) {
					return _x(
						'Bluesky image should be smaller than 1MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'threads' as const,
		active: true,
		automationFreqs: {
			reshare: 8,
			publication: 2,
		},
		labels: {
			add: _x( 'Add Threads Profile', 'command', 'nelio-content' ),
			name: _x( 'Threads', 'text', 'nelio-content' ),
			settings: _x( 'Threads Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="M35.486 22.247a16.667 16.667 0 0 0-.63-.286c-.37-6.827-4.1-10.735-10.364-10.775h-.085c-3.746 0-6.862 1.6-8.78 4.51l3.445 2.362c1.433-2.174 3.681-2.637 5.337-2.637h.058c2.062.014 3.618.613 4.625 1.782.733.852 1.224 2.028 1.466 3.513-1.828-.31-3.806-.407-5.92-.285-5.955.343-9.783 3.816-9.526 8.642.13 2.448 1.35 4.554 3.434 5.93 1.762 1.163 4.03 1.732 6.39 1.603 3.114-.171 5.557-1.36 7.261-3.532 1.295-1.65 2.114-3.788 2.475-6.483 1.484.896 2.584 2.075 3.192 3.492 1.033 2.409 1.093 6.367-2.137 9.594-2.83 2.827-6.23 4.05-11.372 4.088-5.702-.043-10.015-1.871-12.819-5.436C8.911 34.991 7.554 30.17 7.503 24c.051-6.17 1.408-10.992 4.033-14.329 2.804-3.564 7.117-5.393 12.82-5.436 5.743.043 10.131 1.88 13.042 5.462 1.428 1.757 2.504 3.965 3.214 6.54l4.036-1.076c-.86-3.17-2.213-5.902-4.054-8.167C36.86 2.402 31.403.049 24.37 0h-.029C17.323.049 11.925 2.41 8.3 7.02c-3.226 4.1-4.89 9.808-4.946 16.963v.033c.055 7.154 1.72 12.862 4.946 16.964 3.626 4.61 9.024 6.971 16.042 7.02h.029c6.24-.043 10.638-1.677 14.262-5.297 4.74-4.736 4.598-10.673 3.035-14.318-1.12-2.614-3.258-4.736-6.18-6.138zm-10.774 10.13c-2.61.147-5.322-1.024-5.455-3.534-.1-1.86 1.324-3.936 5.615-4.184.492-.028.974-.042 1.448-.042 1.559 0 3.017.152 4.343.441-.495 6.176-3.395 7.179-5.951 7.319z" />
			</svg>
		),
		limit: 300,
		supports: [
			...DEFAULT_SUPPORTS,
			'hootsuite-connection',
			'video',
			'multi-image',
			'multi-media',
			'link-shortener',
		],
		supportedImageTypes: [ 'image/jpeg', 'image/jpg', 'image/png' ],
		supportedVideoTypes: [ 'video/mp4', 'video/quicktime' ],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if ( megas( media.filesizeInBytes ) > 8 ) {
					return _x(
						'Threads image should be smaller than 8MB',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [ 'video/mp4', 'video/quicktime' ].includes( media.mime )
				) {
					return _x(
						'Unsupported video type in Threads',
						'text',
						'nelio-content'
					);
				}

				if ( media.duration > 300 ) {
					return _x(
						'TikTok video must be shorter than 300 seconds',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},

	{
		id: 'vk' as const,
		active: true,
		automationFreqs: {
			reshare: 1,
			publication: 1,
		},
		labels: {
			add: _x( 'Add VK Profile', 'command', 'nelio-content' ),
			name: _x( 'VK', 'text', 'nelio-content' ),
			settings: _x( 'VK Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 48 48">
				<path d="m25.1 30.55c-7.094 0-11.398-4.9218-11.566-13.1h3.5934c0.1114 6.0079 2.8454 8.5546 4.9389 9.0773v-9.0773h3.4455v5.1852c2.0201-0.22402 4.1357-2.5821 4.8471-5.1852h3.3877c-0.5423 3.2004-2.8454 5.5585-4.4725 6.5319 1.6285 0.78603 4.2485 2.8441 5.2599 6.5686h-3.7257c-0.786-2.4891-2.712-4.4175-5.2965-4.6795v4.6795z" />
			</svg>
		),
		limit: 10000,
		supports: [
			...DEFAULT_SUPPORTS,
			'video',
			'multi-image',
			'multi-media',
			'link-shortener',
		],
		supportedImageTypes: [
			'image/jpeg',
			'image/jpg',
			'image/png',
			'image/gif',
		],
		supportedVideoTypes: [
			'video/x-msvideo',
			'video/mp4',
			'video/3gpp',
			'video/mpeg',
			'video/quicktime',
			'video/x-flv',
			'video/x-ms-wmv',
		],
		requires: DEFAULT_REQUIRES,
		validators: {
			image: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [
						'image/jpeg',
						'image/jpg',
						'image/png',
						'image/gif',
					].includes( media.mime )
				) {
					return _x(
						'Unsupported image type in VK',
						'text',
						'nelio-content'
					);
				}

				if ( megas( media.filesizeInBytes ) > 50 ) {
					return _x(
						'VK image should be smaller than 50MB',
						'text',
						'nelio-content'
					);
				}

				if ( media.height + media.width > 14000 ) {
					return _x(
						'Sum of the height and width is no more than 14,000 pixels for VK image',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
			video: ( media ) => {
				if ( ! media ) {
					return undefined;
				}

				if (
					! [
						'video/x-msvideo', // AVI
						'video/mp4', // MP4
						'video/3gpp', // 3GP
						'video/mpeg', // MPEG
						'video/quicktime', // MOV
						'video/x-flv', // FLV
						'video/x-ms-wmv', // WMV
					].includes( media.mime )
				) {
					return _x(
						'Unsupported video type in VK',
						'text',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
		kinds: [
			{
				id: 'single',
				label: _x( 'Personal', 'vk profile', 'nelio-content' ),
			},
			{
				id: 'group',
				label: _x( 'Community', 'vk profile', 'nelio-content' ),
			},
		],
	},

	{
		id: 'plurk' as const,
		active: true,
		automationFreqs: {
			reshare: 8,
			publication: 2,
		},
		labels: {
			add: _x( 'Add Plurk Profile', 'command', 'nelio-content' ),
			name: _x( 'Plurk', 'text', 'nelio-content' ),
			settings: _x( 'Plurk Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 24 24">
				<path d="M6.81 6.175a8.503 8.503 0 0 1 14.2 2.971c.535.009.845-.076 1.135-.302.137-.106.355-.022.355.15v.374c0 1.173-1.036 1.842-1.036 1.842a8.46 8.46 0 0 1-1.297 5.363l.842.843a.5.5 0 0 1 0 .707l-1.52 1.52a.5.5 0 0 1-.707 0l-.774-.774A8.46 8.46 0 0 1 13 20.5a8.47 8.47 0 0 1-5.539-2.052l-.99.857a.5.5 0 0 1-.722-.07l-1.374-1.77a.5.5 0 0 1 .09-.703l1.02-.787a8.5 8.5 0 0 1-.605-1.453c2.139 1.529 3.747-1.782 4.075-3.497.3-1.568.19-5.025-2.145-4.85M1.5 9.5a1 1 0 0 0 1.866.5c.084.233.299.604.654.866-.022.35-.01.707.042 1.036.052.323.146.649.306.922.163.276.412.53.774.629.63.171 1.269-.111 1.754-.553.498-.453.913-1.133 1.138-1.956.224-.82.23-1.687.028-2.39-.195-.68-.642-1.358-1.425-1.462-.793-.104-1.353.456-1.696.983a5.5 5.5 0 0 0-.514 1.045c-.253-.044-.493-.147-.38-.282a1 1 0 1 0-1.408-.328 1 1 0 0 0-1.14.99Zm3.55 2.245a3.6 3.6 0 0 1-.04-.58c1.067-.035 1.154-.777 1.154-1.165v-.008c0-.31 0-.88-.62-.948q.11-.23.235-.423c.293-.45.539-.562.727-.537.196.026.443.218.595.746.144.505.15 1.184-.032 1.85-.181.664-.506 1.171-.845 1.48-.353.32-.65.374-.818.328-.037-.01-.1-.044-.175-.17a1.7 1.7 0 0 1-.181-.573" />
			</svg>
		),
		limit: 280,
		supports: [
			'automations',
			'network-template',
			'profile-template',
			'recurrence',
			'related-post',
			'reusability',
			'text',
			'preview',
			'link-shortener',
		],
		requires: DEFAULT_REQUIRES,
	},

	{
		id: 'discord' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Discord Channel', 'command', 'nelio-content' ),
			name: _x( 'Discord', 'text', 'nelio-content' ),
			settings: _x( 'Discord Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 24 24">
				<path d="M19.27 5.33C17.94 4.71 16.5 4.26 15 4a.1.1 0 0 0-.07.03c-.18.33-.39.76-.53 1.09a16.1 16.1 0 0 0-4.8 0c-.14-.34-.35-.76-.54-1.09c-.01-.02-.04-.03-.07-.03c-1.5.26-2.93.71-4.27 1.33c-.01 0-.02.01-.03.02c-2.72 4.07-3.47 8.03-3.1 11.95c0 .02.01.04.03.05c1.8 1.32 3.53 2.12 5.24 2.65c.03.01.06 0 .07-.02c.4-.55.76-1.13 1.07-1.74c.02-.04 0-.08-.04-.09c-.57-.22-1.11-.48-1.64-.78c-.04-.02-.04-.08-.01-.11c.11-.08.22-.17.33-.25c.02-.02.05-.02.07-.01c3.44 1.57 7.15 1.57 10.55 0c.02-.01.05-.01.07.01c.11.09.22.17.33.26c.04.03.04.09-.01.11c-.52.31-1.07.56-1.64.78c-.04.01-.05.06-.04.09c.32.61.68 1.19 1.07 1.74c.03.01.06.02.09.01c1.72-.53 3.45-1.33 5.25-2.65c.02-.01.03-.03.03-.05c.44-4.53-.73-8.46-3.1-11.95c-.01-.01-.02-.02-.04-.02M8.52 14.91c-1.03 0-1.89-.95-1.89-2.12s.84-2.12 1.89-2.12c1.06 0 1.9.96 1.89 2.12c0 1.17-.84 2.12-1.89 2.12m6.97 0c-1.03 0-1.89-.95-1.89-2.12s.84-2.12 1.89-2.12c1.06 0 1.9.96 1.89 2.12c0 1.17-.83 2.12-1.89 2.12" />
			</svg>
		),
		limit: 2000,
		supports: [ ...DEFAULT_SUPPORTS, 'multi-image', 'link-shortener' ],
		supportedImageTypes: [ 'image' ],
		requires: DEFAULT_REQUIRES,
	},

	{
		id: 'ok' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add OK.ru Profile', 'command', 'nelio-content' ),
			name: _x( 'OK.ru', 'text', 'nelio-content' ),
			settings: _x( 'OK.ru Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 24 24">
				<path d="M17.83,12.74C17.55,12.17 16.76,11.69 15.71,12.5C14.28,13.64 12,13.64 12,13.64C12,13.64 9.72,13.64 8.29,12.5C7.24,11.69 6.45,12.17 6.17,12.74C5.67,13.74 6.23,14.23 7.5,15.04C8.59,15.74 10.08,16 11.04,16.1L10.24,16.9C9.1,18.03 8,19.12 7.25,19.88C6.8,20.34 6.8,21.07 7.25,21.5L7.39,21.66C7.84,22.11 8.58,22.11 9.03,21.66L12,18.68C13.15,19.81 14.24,20.9 15,21.66C15.45,22.11 16.18,22.11 16.64,21.66L16.77,21.5C17.23,21.07 17.23,20.34 16.77,19.88L13.79,16.9L13,16.09C13.95,16 15.42,15.73 16.5,15.04C17.77,14.23 18.33,13.74 17.83,12.74M12,4.57C13.38,4.57 14.5,5.69 14.5,7.06C14.5,8.44 13.38,9.55 12,9.55C10.62,9.55 9.5,8.44 9.5,7.06C9.5,5.69 10.62,4.57 12,4.57M12,12.12C14.8,12.12 17.06,9.86 17.06,7.06C17.06,4.27 14.8,2 12,2C9.2,2 6.94,4.27 6.94,7.06C6.94,9.86 9.2,12.12 12,12.12Z" />
			</svg>
		),
		limit: 3000,
		supports: [
			...DEFAULT_SUPPORTS,
			'multi-image',
			'link-shortener',
			/* 'video'*/
		],
		supportedImageTypes: [ 'image' ],
		//supportedVideoTypes: [ 'video' ],
		requires: DEFAULT_REQUIRES,
		// validators: {
		// 	video: ( media ) => {
		// 		if ( ! media ) {
		// 			return undefined;
		// 		} //end if

		// 		if ( kilos( media.filesizeInBytes ) < 16 ) {
		// 			return _x(
		// 				'OK.ru video must be at least 16KB',
		// 				'text',
		// 				'nelio-content'
		// 			);
		// 		} //end if

		// 		if ( megas( media.filesizeInBytes ) > 1024 ) {
		// 			return _x(
		// 				'OK.ru video should be smaller than 1GB',
		// 				'text',
		// 				'nelio-content'
		// 			);
		// 		} //end if

		// 		return undefined;
		// 	},
		// },
		kinds: [
			{
				id: 'single',
				label: _x( 'Personal', 'ok.ru profile', 'nelio-content' ),
			},
			{
				id: 'group',
				label: _x( 'Group', 'ok.ru profile', 'nelio-content' ),
			},
		],
	},

	{
		id: 'slack' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Slack Channel', 'command', 'nelio-content' ),
			name: _x( 'Slack', 'text', 'nelio-content' ),
			settings: _x( 'Slack Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 24 24">
				<path d="M6,15A2,2 0 0,1 4,17A2,2 0 0,1 2,15A2,2 0 0,1 4,13H6V15M7,15A2,2 0 0,1 9,13A2,2 0 0,1 11,15V20A2,2 0 0,1 9,22A2,2 0 0,1 7,20V15M9,7A2,2 0 0,1 7,5A2,2 0 0,1 9,3A2,2 0 0,1 11,5V7H9M9,8A2,2 0 0,1 11,10A2,2 0 0,1 9,12H4A2,2 0 0,1 2,10A2,2 0 0,1 4,8H9M17,10A2,2 0 0,1 19,8A2,2 0 0,1 21,10A2,2 0 0,1 19,12H17V10M16,10A2,2 0 0,1 14,12A2,2 0 0,1 12,10V5A2,2 0 0,1 14,3A2,2 0 0,1 16,5V10M14,18A2,2 0 0,1 16,20A2,2 0 0,1 14,22A2,2 0 0,1 12,20V18H14M14,17A2,2 0 0,1 12,15A2,2 0 0,1 14,13H19A2,2 0 0,1 21,15A2,2 0 0,1 19,17H14Z" />
			</svg>
		),
		limit: 2000,
		supports: [ ...DEFAULT_SUPPORTS, 'multi-image', 'link-shortener' ],
		supportedImageTypes: [ 'image' ],
		requires: DEFAULT_REQUIRES,
	},

	{
		id: 'band' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Band', 'command', 'nelio-content' ),
			name: _x( 'Band', 'text', 'nelio-content' ),
			settings: _x( 'Band Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 24 24">
				<path
					d="M16.028 14.023a4.046 4.046 0 0 1-4.046 4.047 4.046 4.046 0 0 1-4.046-4.047 4.046 4.046 0 0 1 4.046-4.046 4.046 4.046 0 0 1 4.046 4.046zm-8.092-7.01a8.094 8.094 0 1 1-4.047 7.01m4.047 0V3.907a2.023 2.023 0 0 0-2.024-2.023A2.023 2.023 0 0 0 3.89 3.907v10.116"
					strokeWidth="2"
				/>
			</svg>
		),
		limit: 2000,
		supports: [
			'automations',
			'network-template',
			'preview',
			'profile-template',
			'recurrence',
			'related-post',
			'reusability',
			'text',
			'link-shortener',
		],
		requires: DEFAULT_REQUIRES,
	},

	{
		id: 'webhook' as const,
		active: true,
		automationFreqs: BASIC_AUTOMATION_FREQS,
		labels: {
			add: _x( 'Add Webhook', 'command', 'nelio-content' ),
			name: _x( 'Webhook', 'text', 'nelio-content' ),
			settings: _x( 'Webhook Settings', 'text', 'nelio-content' ),
		},
		icon: (
			<svg viewBox="0 0 24 24">
				<path d="M10.46,19C9,21.07 6.15,21.59 4.09,20.15C2.04,18.71 1.56,15.84 3,13.75C3.87,12.5 5.21,11.83 6.58,11.77L6.63,13.2C5.72,13.27 4.84,13.74 4.27,14.56C3.27,16 3.58,17.94 4.95,18.91C6.33,19.87 8.26,19.5 9.26,18.07C9.57,17.62 9.75,17.13 9.82,16.63V15.62L15.4,15.58L15.47,15.47C16,14.55 17.15,14.23 18.05,14.75C18.95,15.27 19.26,16.43 18.73,17.35C18.2,18.26 17.04,18.58 16.14,18.06C15.73,17.83 15.44,17.46 15.31,17.04L11.24,17.06C11.13,17.73 10.87,18.38 10.46,19M17.74,11.86C20.27,12.17 22.07,14.44 21.76,16.93C21.45,19.43 19.15,21.2 16.62,20.89C15.13,20.71 13.9,19.86 13.19,18.68L14.43,17.96C14.92,18.73 15.75,19.28 16.75,19.41C18.5,19.62 20.05,18.43 20.26,16.76C20.47,15.09 19.23,13.56 17.5,13.35C16.96,13.29 16.44,13.36 15.97,13.53L15.12,13.97L12.54,9.2H12.32C11.26,9.16 10.44,8.29 10.47,7.25C10.5,6.21 11.4,5.4 12.45,5.44C13.5,5.5 14.33,6.35 14.3,7.39C14.28,7.83 14.11,8.23 13.84,8.54L15.74,12.05C16.36,11.85 17.04,11.78 17.74,11.86M8.25,9.14C7.25,6.79 8.31,4.1 10.62,3.12C12.94,2.14 15.62,3.25 16.62,5.6C17.21,6.97 17.09,8.47 16.42,9.67L15.18,8.95C15.6,8.14 15.67,7.15 15.27,6.22C14.59,4.62 12.78,3.85 11.23,4.5C9.67,5.16 8.97,7 9.65,8.6C9.93,9.26 10.4,9.77 10.97,10.11L11.36,10.32L8.29,15.31C8.32,15.36 8.36,15.42 8.39,15.5C8.88,16.41 8.54,17.56 7.62,18.05C6.71,18.54 5.56,18.18 5.06,17.24C4.57,16.31 4.91,15.16 5.83,14.67C6.22,14.46 6.65,14.41 7.06,14.5L9.37,10.73C8.9,10.3 8.5,9.76 8.25,9.14Z" />
			</svg>
		),
		limit: 10000,
		supports: [ 'automations', 'related-post', 'reusability' ],
		requires: [],
		validators: {
			general: ( attrs ) => {
				if (
					attrs.network === 'webhook' &&
					! isUrl( attrs.webhookSettings?.url )
				) {
					return _x(
						'Provide a fully qualified URL to send the webhook data',
						'user',
						'nelio-content'
					);
				}

				if (
					attrs.network === 'webhook' &&
					attrs.webhookSettings?.bodyType === 'json' &&
					! isValidJSON( attrs.webhookSettings?.bodyContent )
				) {
					return _x(
						'Provide a valid JSON body for the webhook',
						'user',
						'nelio-content'
					);
				}

				return undefined;
			},
		},
	},
];

const ACTIVE_NETWORKS = filter( NETWORKS, { active: true } );

const MAX_CHAR_LIMIT: number = Math.max( ...map( ACTIVE_NETWORKS, 'limit' ) );
const MIN_CHAR_LIMIT: number = Math.min( ...map( ACTIVE_NETWORKS, 'limit' ) );

type TargetLabel = {
	readonly explanation: string;
	readonly loading: string;
	readonly noTargetsExplanation: string;
	readonly selectTarget: string;
	readonly selectTargetError: string;
	readonly selectTargets: string;
	readonly targetLabel: string;
	readonly title: string;
};

const TARGET_LABELS: Record< string, TargetLabel > = {
	default: {
		explanation: _x(
			'Please select where your message will be shared on:',
			'user',
			'nelio-content'
		),
		loading: _x( 'Loading…', 'text', 'nelio-content' ),
		noTargetsExplanation: _x(
			'Your profile does not have any locations yet. Please configure one first.',
			'user',
			'nelio-content'
		),
		selectTarget: _x( 'Select a location…', 'user', 'nelio-content' ),
		selectTargets: _x( 'Select locations…', 'user', 'nelio-content' ),
		selectTargetError: _x(
			'Please select a location',
			'user',
			'nelio-content'
		),
		targetLabel: _x( 'Location', 'text', 'nelio-content' ),
		title: _x( 'Select Location', 'text', 'nelio-content' ),
	},

	pinterest: {
		explanation: _x(
			'Please select the boards your message will be shared on:',
			'user (pinterest boards)',
			'nelio-content'
		),
		loading: _x(
			'Loading boards…',
			'text (pinterest boards)',
			'nelio-content'
		),
		noTargetsExplanation: _x(
			'Your Pinterest profile does not have any boards yet. Go to Pinterest and create one first.',
			'user (no pinterest boards)',
			'nelio-content'
		),
		selectTarget: _x( 'Select a board…', 'user', 'nelio-content' ),
		selectTargets: _x( 'Select boards…', 'user', 'nelio-content' ),
		selectTargetError: _x(
			'Please select a board',
			'user',
			'nelio-content'
		),
		targetLabel: _x(
			'Board',
			'text (pinterest target name)',
			'nelio-content'
		),
		title: _x(
			'Select Boards',
			'text (pinterest boards)',
			'nelio-content'
		),
	},

	reddit: {
		explanation: _x(
			'Please select where your message will be shared on:',
			'user',
			'nelio-content'
		),
		loading: _x( 'Loading…', 'text', 'nelio-content' ),
		noTargetsExplanation: _x(
			'Your profile does not have any subreddits yet. Please configure one first.',
			'user',
			'nelio-content'
		),
		selectTarget: _x( 'Select a subreddit…', 'user', 'nelio-content' ),
		selectTargets: _x( 'Select subreddits…', 'user', 'nelio-content' ),
		selectTargetError: _x(
			'Please select a subreddit',
			'user',
			'nelio-content'
		),
		targetLabel: _x( 'Subreddit', 'text', 'nelio-content' ),
		title: _x( 'Select Subreddit', 'text', 'nelio-content' ),
	},
};

const _supportedNetworks = map( ACTIVE_NETWORKS, 'id' );
export const getSupportedNetworks = (): Array< SocialNetworkName > =>
	_supportedNetworks;

const _networksById = keyBy( ACTIVE_NETWORKS, 'id' );
export const getCharLimitInNetwork = (
	networkName: SocialNetworkName
): number => {
	const network = _networksById[ networkName ] || { limit: MAX_CHAR_LIMIT };
	return network.limit;
};

export const getMinCharLimit = (): number => MIN_CHAR_LIMIT;

export const getMaxCharLimit = (): number => MAX_CHAR_LIMIT;

export const getTagsLimitInNetwork = (
	networkName: SocialNetworkName
): number => {
	const network = _networksById[ networkName ] || { tagsLimit: 0 };
	return network.tagsLimit ?? 0;
};

const LINK_25CH = 'aaaabaaaabaaaabaaaabaaaab';
export function getMessageLength( text: string ): number {
	text = ` ${ text } `;
	text = text.replaceAll( /(\s)https?:\/\/[^\s]+/g, `$1${ LINK_25CH }` );
	text = text.trim();
	return twitter.parseTweet( text ).weightedLength;
}

export const getDefaultPublicationValue = (
	network: SocialNetworkName
): number => _networksById[ network ]?.automationFreqs?.publication || 0;

export const getDefaultReshareValue = ( network: SocialNetworkName ): number =>
	_networksById[ network ]?.automationFreqs?.reshare || 0;

export const doesNetworkSupport = (
	support: SocialNetworkSupport,
	networkName: Maybe< SocialNetworkName >
): boolean =>
	!! networkName &&
	!! _networksById[ networkName ]?.supports.includes( support );

export const doesNetworkRequire = (
	support: SocialNetworkSupport,
	networkName: Maybe< SocialNetworkName >
): boolean =>
	!! networkName &&
	!! _networksById[ networkName ]?.requires.some( ( r ) =>
		r.includes( support )
	);

export const getNetworkLabel = (
	label: keyof SocialNetwork[ 'labels' ],
	networkName: SocialNetworkName
): string => {
	const network = _networksById[ networkName ];
	return network?.labels[ label ] || '';
};

export const getSupportedImageTypes = (
	networkName: SocialNetworkName
): ReadonlyArray< string > => {
	const network = _networksById[ networkName ];
	return network?.supportedImageTypes ?? [];
};

export const getSupportedVideoTypes = (
	networkName: SocialNetworkName
): ReadonlyArray< string > => {
	const network = _networksById[ networkName ];
	return network?.supportedVideoTypes ?? [];
};

export const getTargetLabel = (
	label: keyof TargetLabel,
	networkName: SocialNetworkName
): string => {
	const labels = TARGET_LABELS[ networkName ] || TARGET_LABELS.default;
	return labels?.[ label ] || '';
};

export const getNetworkKinds = (
	networkName: SocialNetworkName
): ReadonlyArray< SocialKind > => {
	const network = _networksById[ networkName ];
	return network?.kinds || [];
};

export const getNetworkIcon = ( networkName: SocialNetworkName ): JSX.Element =>
	_networksById[ networkName ]?.icon || (
		<svg viewBox="0 0 48 84">
			<circle cx="24" cy="24" r="12" />
		</svg>
	);

export function getRequirementsError(
	attrs: RequirementsErrorAttributes
): Maybe< string > {
	const network = _networksById[ attrs.network ];
	if ( ! network ) {
		return _x( 'Unknown error', 'text', 'nelio-content' );
	}

	const getReqErr = ( req: SocialNetworkSupport ): string => {
		switch ( req ) {
			case 'image':
			case 'multi-image':
				if (
					[ 'image', 'auto-image' ].includes( attrs.type ) ||
					( network.supports.includes( 'video' ) &&
						'video' === attrs.type ) ||
					( network.supports.includes( 'multi-media' ) &&
						'multi-media' === attrs.type )
				) {
					return '';
				}

				return network.supports.includes( 'video' ) ||
					network.supports.includes( 'multi-media' )
					? GENERIC_ERRORS.missingMedia
					: GENERIC_ERRORS.missingImage;

			case 'multi-media':
				if (
					[ 'image', 'auto-image', 'video', 'multi-media' ].includes(
						attrs.type
					)
				) {
					return '';
				}

				return GENERIC_ERRORS.missingMedia;

			case 'multi-target':
				return ! attrs.targetName
					? getTargetLabel( 'selectTargetError', attrs.network )
					: '';

			case 'text':
				if ( attrs.textComputed === '' ) {
					return GENERIC_ERRORS.missingText;
				}

				return getMessageLength( attrs.textComputed ) > network.limit
					? GENERIC_ERRORS.textTooLong
					: '';

			case 'video':
				if (
					'video' === attrs.type ||
					( network.supports.includes( 'image' ) &&
						'image' === attrs.type ) ||
					( network.supports.includes( 'multi-media' ) &&
						'multi-media' === attrs.type )
				) {
					return '';
				}

				return network.supports.includes( 'image' )
					? GENERIC_ERRORS.missingMedia
					: GENERIC_ERRORS.missingVideo;

			case 'related-post':
				if ( attrs.postId ) {
					return '';
				}

				return GENERIC_ERRORS.missingRelatedPost;

			case 'automations':
			case 'buffer-connection':
			case 'hootsuite-connection':
			case 'link-shortener':
			case 'medium-tags':
			case 'network-image':
			case 'network-template':
			case 'preview':
			case 'profile-template':
			case 'recurrence':
			case 'reusability':
				return '';
		}
	};

	const getMediaErr = (): string => {
		if ( network.supports.includes( 'image' ) && 'image' === attrs.type ) {
			return isArray( attrs.image )
				? attrs.image
						.map(
							( img: MediaItem ) =>
								network.validators?.image?.( img ) ?? ''
						)
						.filter( ( m ) => !! m )[ 0 ] ??
						network.validators?.images?.( attrs.image ) ??
						''
				: network.validators?.image?.(
						attrs.image as Maybe< MediaItem >
				  ) ?? '';
		}

		if ( network.supports.includes( 'video' ) && 'video' === attrs.type ) {
			return isArray( attrs.video )
				? attrs.video
						.map(
							( v: MediaItem ) =>
								network.validators?.video?.( v ) ?? ''
						)
						.filter( ( v ) => !! v )[ 0 ] ?? ''
				: network.validators?.video?.(
						attrs.video as Maybe< MediaItem >
				  ) ?? '';
		}

		return '';
	};

	const getNetworkErr = (): string =>
		network.validators?.general?.( attrs ) ?? '';

	const errors = network.requires.map( ( reqs ) =>
		reqs.reduce( ( e, r ) => e || getReqErr( r ), '' )
	);

	/* eslint-disable @wordpress/valid-sprintf */
	const err = sprintf(
		find( errors, ( e ) => e !== '' ) ?? '',
		network.labels.name
	);
	/* eslint-enable @wordpress/valid-sprintf */
	return err || getMediaErr() || getNetworkErr() || undefined;
}

// =======
// HELPERS
// =======

const kilos = ( bytes: number ) => Math.round( bytes / 1024 );

const megas = ( bytes: number ) => Math.round( kilos( bytes ) / 1024 );

const URL_REGEX =
	/^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/;
const isUrl = ( url?: string | Url ): url is Url => {
	return !! url && URL_REGEX.test( url );
};

const isValidJSON = ( value?: string ): boolean => {
	if ( value === undefined ) {
		return false;
	}

	try {
		JSON.parse( value );
		return true;
	} catch {
		return false;
	}
};
