import { equiv } from "@thi.ng/equiv" ; import { illegalState } from "@thi.ng/errors/illegal-state" ; import type { Attribs , IHiccupShape , PathSegment } from "@thi.ng/geom-api" ; import { copy } from "@thi.ng/vectors/copy" ; import { __copyAttribs } from "../internal/copy.js" ; export class Path implements IHiccupShape { closed = false ; constructor ( public segments = [ ] , public attribs ? ) { } get type ( ) { return "path" ; } * [ Symbol . iterator ] ( ) { yield * this . segments ; } copy ( ) { const p = new Path ( this . segments . map ( ( s ) => { const d = { type : s . type } ; s . point && ( d . point = copy ( s . point ) ) ; s . geo && ( d . geo = < any > s . geo . copy ( ) ) ; return d ; } ) , __copyAttribs ( this ) ) ; p . closed = this . closed ; return p ; } equiv ( o ) { return o instanceof Path && equiv ( this . segments , o . segments ) ; } add ( s ) { if ( this . closed ) illegalState ( "path already closed" ) ; this . segments . push ( s ) ; } toHiccup ( ) { let dest = [ ] ; const segments = this . segments ; const n = segments . length ; if ( n > 1 ) { for ( let i = 0 ; i < n ; i ++ ) { const s = segments [ i ] ; if ( s . geo ) { dest = dest . concat ( s . geo ! . toHiccupPathSegments ( ) ) ; } else if ( s . point ) { dest . push ( [ "M" , s . point ] ) ; } } if ( this . closed ) { dest . push ( [ "Z" ] ) ; } } return [ "path" , this . attribs || { } , dest ] ; } }