{"version":3,"file":"sort-children.mjs","sources":["../../../src/core/utils/sort-children.ts"],"sourcesContent":["import { type Container } from 'pixi.js';\nimport { type OverflowContainer } from '../../components/LayoutContainer';\nimport { type Layout } from '../Layout';\n\n/**\n * Sorts the children of the layout based on their order in the parent container\n * syncing the Yoga tree with the Pixi display list.\n */\nexport function onChildAdded(layout: Layout, pixiParent: Container) {\n    let parentLayout = pixiParent.layout;\n    let overflowContainer: Container | undefined;\n\n    // If inside an overflow container, actual yoga parent is the overflow's parent layout.\n    if (!parentLayout && (pixiParent as OverflowContainer).isOverflowContainer) {\n        overflowContainer = pixiParent;\n        parentLayout = overflowContainer.parent?.layout ?? null;\n        pixiParent = overflowContainer.parent!;\n    }\n\n    if (!parentLayout) {\n        return false;\n    }\n\n    // Detach from previous yoga parent if any\n    const yogaParent = layout.yoga.getParent();\n\n    if (yogaParent) {\n        yogaParent.removeChild(layout.yoga);\n    }\n\n    const yogaIndex = computeYogaInsertionIndex(layout, pixiParent, overflowContainer);\n\n    if (yogaIndex === -1) {\n        return false;\n    }\n\n    // Fast append path\n    if (yogaIndex === parentLayout.yoga.getChildCount()) {\n        parentLayout.yoga.insertChild(layout.yoga, yogaIndex);\n\n        return true;\n    }\n\n    parentLayout.yoga.insertChild(layout.yoga, yogaIndex);\n\n    return true;\n}\n\n/**\n * Computes the Yoga insertion index with a single pass over the logical (flattened if needed) sibling list.\n * @param layout - layout being inserted\n * @param parent - the real parent container whose layout we insert into\n * @param overflow - optional overflow container that actually contains the target\n */\nfunction computeYogaInsertionIndex(layout: Layout, parent: Container, overflow?: Container): number {\n    const target = layout.target;\n    let index = 0;\n\n    if (overflow) {\n        // Iterate real parent children; when reaching overflow, iterate its children inline.\n        for (const child of parent.children) {\n            if (child === overflow) {\n                for (const inner of overflow.children) {\n                    if (!inner.layout || !inner.visible) continue;\n                    if (inner === target) return index;\n                    index++;\n                }\n\n                return -1; // target not found inside overflow\n            }\n            if (child.layout && child.visible) {\n                index++;\n            }\n        }\n\n        return -1;\n    }\n    for (const child of parent.children) {\n        if (!child.layout || !child.visible) continue;\n        if (child === target) return index;\n        index++;\n    }\n\n    return -1;\n}\n\n/**\n * Removes the child from the layout\n */\nexport function onChildRemoved(layout: Layout) {\n    const yogaParent = layout.yoga.getParent();\n\n    if (yogaParent) {\n        yogaParent.removeChild(layout.yoga);\n    }\n}\n"],"names":[],"mappings":"AAQgB,SAAA,aAAa,QAAgB,YAAuB;AAApD;AACZ,MAAI,eAAe,WAAW;AAC1B,MAAA;AAGA,MAAA,CAAC,gBAAiB,WAAiC,qBAAqB;AACpD,wBAAA;AACL,qBAAA,uBAAkB,WAAlB,mBAA0B,WAAU;AACnD,iBAAa,kBAAkB;AAAA,EAAA;AAGnC,MAAI,CAAC,cAAc;AACR,WAAA;AAAA,EAAA;AAIL,QAAA,aAAa,OAAO,KAAK,UAAU;AAEzC,MAAI,YAAY;AACD,eAAA,YAAY,OAAO,IAAI;AAAA,EAAA;AAGtC,QAAM,YAAY,0BAA0B,QAAQ,YAAY,iBAAiB;AAEjF,MAAI,cAAc,IAAI;AACX,WAAA;AAAA,EAAA;AAIX,MAAI,cAAc,aAAa,KAAK,cAAA,GAAiB;AACjD,iBAAa,KAAK,YAAY,OAAO,MAAM,SAAS;AAE7C,WAAA;AAAA,EAAA;AAGX,eAAa,KAAK,YAAY,OAAO,MAAM,SAAS;AAE7C,SAAA;AACX;AAQA,SAAS,0BAA0B,QAAgB,QAAmB,UAA8B;AAChG,QAAM,SAAS,OAAO;AACtB,MAAI,QAAQ;AAEZ,MAAI,UAAU;AAEC,eAAA,SAAS,OAAO,UAAU;AACjC,UAAI,UAAU,UAAU;AACT,mBAAA,SAAS,SAAS,UAAU;AACnC,cAAI,CAAC,MAAM,UAAU,CAAC,MAAM,QAAS;AACjC,cAAA,UAAU,OAAe,QAAA;AAC7B;AAAA,QAAA;AAGG,eAAA;AAAA,MAAA;AAEP,UAAA,MAAM,UAAU,MAAM,SAAS;AAC/B;AAAA,MAAA;AAAA,IACJ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