import { callable, isNullish } from 'mixlea-utils-js'; import type { Pipeline, PipelineState } from '../pipeline'; import { MlButtonList, type MlButtonListProps } from '../../../ml-button-list'; export interface FooterLeftButtonListExtensionConfig { defaultState?: any; getProps: (opts: { formData: PipelineState['formData']; headerLeftSlotState: PipelineState['headerLeftSlot']; headerRightSlotState: PipelineState['headerRightSlot']; footerLeftSlotState: PipelineState['footerLeftSlot']; footerRightSlotState: PipelineState['footerRightSlot']; headerLeftButtonListState: PipelineState['headerLeftButtonList']; headerRightButtonListState: PipelineState['headerRightButtonList']; footerLeftButtonListState: PipelineState['footerLeftButtonList']; footerRightButtonListState: PipelineState['footerRightButtonList']; setStateAtKey: Pipeline['setStateAtKey']; setPartialState: Pipeline['setPartialState']; }) => MlButtonListProps; onChange?: (nextState: any) => void; } export function footerLeftButtonListExtension(config?: FooterLeftButtonListExtensionConfig) { if (isNullish(config)) { return (pipeline: Pipeline) => pipeline; } return function (pipeline: Pipeline) { const getProps = config.getProps; const onChange = config.onChange; const pipelineState = pipeline.getState(); const setStateAtKey: Pipeline['setStateAtKey'] = (key, partialState) => pipeline.setStateAtKey(key, partialState).then((pipelineState) => { if (key === 'footerLeftButtonList') { onChange?.(pipelineState.footerLeftButtonList); } return pipelineState; }); const setPartialState: Pipeline['setPartialState'] = (partialState) => { let isChange = false; return pipeline .setPartialState((prevState) => { const nextPartialState = callable(partialState, prevState); if ('footerLeftButtonList' in nextPartialState) { isChange = true; } return nextPartialState; }) .then((pipelineState) => { if (isChange) { onChange?.(pipelineState.footerLeftButtonList); } return pipelineState; }); }; pipeline.appendFooterLeftSlots(() => { const buttonListProps = getProps({ formData: pipelineState.formData, headerLeftSlotState: pipelineState.headerLeftSlot, headerRightSlotState: pipelineState.headerRightSlot, footerLeftSlotState: pipelineState.footerLeftSlot, footerRightSlotState: pipelineState.footerRightSlot, headerLeftButtonListState: pipelineState.headerLeftButtonList, headerRightButtonListState: pipelineState.headerRightButtonList, footerLeftButtonListState: pipelineState.footerLeftButtonList, footerRightButtonListState: pipelineState.footerRightButtonList, setStateAtKey, setPartialState, }); return ; }); return pipeline; }; }