/* jscpd:ignore-start */
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import c from 'chalk';
import { glob } from 'glob';
import * as path from 'path';
import fs from 'fs-extra';
import { uxLog } from '../../../../common/utils/index.js';
import { GLOB_IGNORE_PATTERNS } from '../../../../common/utils/projectUtils.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('sfdx-hardis', 'org');
export default class CleanFlowPositions extends SfCommand {
    static title = 'Clean Flow Positions';
    static description = `Replace all positions in Auto-Layout Flows by 0 to simplify conflicts management

As Flows are defined as Auto-Layout, the edition in Setup UI is not impacted.
  
Before:

\`\`\`xml
<locationX>380</locationX>
<locationY>259</locationY>
\`\`\`

After:

\`\`\`xml
<locationX>0</locationX>
<locationY>0</locationY>
\`\`\`

Can be automated at each **hardis:work:save** if **flowPositions** is added in .sfdx-hardis.yml **autoCleanTypes** property  

Example in config/.sfdx-hardis.yml:

\`\`\`yaml
autoCleanTypes:
  - destructivechanges
  - flowPositions
\`\`\`
`;
    static examples = ['$ sf hardis:project:clean:flowpositions'];
    static flags = {
        folder: Flags.string({
            char: 'f',
            default: 'force-app',
            description: 'Root folder',
        }),
        debug: Flags.boolean({
            char: 'd',
            default: false,
            description: messages.getMessage('debugMode'),
        }),
        websocket: Flags.string({
            description: messages.getMessage('websocket'),
        }),
        skipauth: Flags.boolean({
            description: 'Skip authentication check when a default username is required',
        }),
    };
    // Set this to true if your command requires a project workspace; 'requiresProject' is false by default
    static requiresProject = true;
    folder;
    debugMode = false;
    async run() {
        const { flags } = await this.parse(CleanFlowPositions);
        this.folder = flags.folder || './force-app';
        this.debugMode = flags.debug || false;
        // Delete standard files when necessary
        uxLog("action", this, c.cyan(`Setting flows as Auto Layout and remove positions...`));
        /* jscpd:ignore-end */
        const rootFolder = path.resolve(this.folder);
        const findManagedPattern = rootFolder + `/**/*.flow-meta.xml`;
        const matchingFlows = await glob(findManagedPattern, { cwd: process.cwd(), ignore: GLOB_IGNORE_PATTERNS });
        let counter = 0;
        for (const flowMetadataFile of matchingFlows) {
            const flowXml = await fs.readFile(flowMetadataFile, 'utf8');
            if (flowXml.includes('<stringValue>AUTO_LAYOUT_CANVAS</stringValue>')) {
                let updatedFlowXml = flowXml.replace(/<locationX>([0-9]*)<\/locationX>/gm, '<locationX>0</locationX>');
                updatedFlowXml = updatedFlowXml.replace(/<locationY>([0-9]*)<\/locationY>/gm, '<locationY>0</locationY>');
                if (updatedFlowXml !== flowXml) {
                    await fs.writeFile(flowMetadataFile, updatedFlowXml);
                    counter++;
                    uxLog("log", this, c.grey(`Removed positions from Flow ${flowMetadataFile}`));
                }
            }
        }
        // Summary
        const msg = `Updated ${c.green(c.bold(counter))} flows to remove positions`;
        uxLog("action", this, c.cyan(msg));
        // Return an object to be displayed with --json
        return { outputString: msg };
    }
}
//# sourceMappingURL=flowpositions.js.map