namespace $.$$ {
	$hyoo_crus_yard.masters = [
		$mol_dom_context.document.location.origin +'/',
	]

	export class $hd_canary_app_monitor extends $.$hd_canary_app_monitor {

		status_total() {
			return this.home().Sites(null)!.remote_list().every( key => key.last_ping_status()?.val() == true) ? "🟢" : "🔴"
		}

		target_status( prop: $hd_canary_app_home_site ) {
			return prop.last_ping_status()?.val() ? "🟢" : "🔴"
		}
		target_title(prop: $hd_canary_app_home_site) {
			return prop.title()!.val() as string
		}
		target_url(prop: $hd_canary_app_home_site) {
			return prop.url()!.val() as string;
		}
		target_moment(prop: $hd_canary_app_home_site) {
			return ((prop.last_ping_time()?.val() as $mol_time_moment) ?? "planed ping").toString('YYYY-MM-DD hh:mm:ss')// ?? prop.ref().description! as any
		}

		target_list() {
			return this.home().Sites(null)!.remote_list().map( key => this.Target( key ) ) ?? []
		}

		is_valid_http_url(string: string) {
			let url;
			try {
			  url = new URL(string);
			} catch (_) {
			  return false;
			}
			return url.protocol === "http:" || url.protocol === "https:";
		}

		add_submit() {
			const url = this.add_url().trim()
			if (url == "") throw "Empty url!"
			if (this.is_valid_http_url(url) == false) throw "Not valid url!"
			

			let title = this.add_title().trim()
			if (title == "") title = url.replace(/(^https?:\/\/)|(\/+$)/g, '');

			this.add_site(url, title)

			this.add_url('');
			this.add_title('');
		}

		@$mol_action
		add_site(url: string, title: string) {
			const new_site = this.home().Sites(null)!.make(null)
			new_site.land().give(null, $hyoo_crus_rank_rule)
			
			new_site.url(null)!.val(url)
			new_site.title(null)!.val(title)
		}

		target_remove(prop: $hd_canary_app_home_site) {
			this.home().Sites(null)!.cut(prop.ref())
		}

		// наш id клиента
		my_key() {
			return this.$.$hyoo_crus_auth.current().lord().description!
		}

		@ $mol_mem
		users() {
			const ref = $hyoo_crus_ref( this.$.$mol_fetch.text( '/join' ) )
			return this.$.$hyoo_crus_glob.Node( ref,  $hd_canary_app_server ).Users(null)!
		}

		@ $mol_mem
		home() {
			// берем свой уголок
			const home = this.$.$hyoo_crus_glob.home( $hd_canary_app_home )
			// home.land().join()

			// проверяем есть ли он в базе
			if (this.users().has(home.ref()) == false) {

				// даем серверу права на запись
				const ref = $hyoo_crus_ref( this.$.$mol_fetch.text( '/ref' ) )
				home.land().give(ref, $hyoo_crus_rank_rule)

				// и заносим это в глобальную базу
				this.users().add(home.ref())

			}

			return home
		}

		@$mol_action
		bulk_add(sites_input: string) {
			const sites = sites_input.split(/\r?\n/).map( site => site.trim()).filter(site => site != "")
			for (const site of sites) {
				this.add_site(site, site.replace(/(^https?:\/\/)|(\/+$)/g, ''))
			}
		}
	}
}
