import { Observable , Subject , finalize , fromEvent , map , tap } from "rxjs" ; import { getLocation } from "~/browser" ; import { Component } from "../../_" ; import { SearchQuery } from "../query" ; /**
 * Search sharing
 */ export interface SearchShare { url : URL ; } /**
 * Watch options
 */ interface WatchOptions { query$ : Observable < SearchQuery > ; } /**
 * Mount options
 */ interface MountOptions { query$ : Observable < SearchQuery > ; } param _el param options returns export function watchSearchShare ( _el , { query$ } ) { return query$ . pipe ( map ( ( { value } ) => { const url = getLocation ( ) ; url . hash = "" ; url . searchParams . delete ( "h" ) ; url . searchParams . set ( "q" , value ) ; return { url } ; } ) ) ; } param el param options returns export function mountSearchShare ( el , options ) { const push$ = new Subject < SearchShare > ( ) ; push$ . subscribe ( ( { url } ) => { el . setAttribute ( "data-clipboard-text" , el . href ) ; el . href = `${ url }` ; } ) ; fromEvent ( el , "click" ) . subscribe ( ev => ev . preventDefault ( ) ) ; return watchSearchShare ( el , options ) . pipe ( tap ( state => push$ . next ( state ) ) , finalize ( ( ) => push$ . complete ( ) ) , map ( state => ( { ref : el , ... state } ) ) ) ; }